Build a set fails when using set(int) (Python)

2020-03-26 12:02发布

问题:

I can do

>>> s = {1}
>>> type(s)
<class 'set'>

but

>>> s = set(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

what is the difference?

回答1:

The difference is that the set() constructor takes an iterable. A single number is not an iterable.

s = set((1,))