Reputation: 5575
I am using Python 2.7, and just wondering if there is any difference between set()
and Set()
(i.e. with/without the capitalization).
Specifically, the Python instructions https://docs.python.org/2/library/sets.html suggest that Sets should be imported and initialized as:
from sets import Set
x = Set()
I have just been using the command set() without importing anything, i.e.:
x = set()
Just wondering if these are identical, or if they are somehow different.
Upvotes: 1
Views: 301
Reputation: 107347
As it says in Python documentation the Set
class provides every set
method except for __hash__()
.
For advanced applications requiring a hash method, the ImmutableSet class adds a hash() method but omits methods which alter the contents of the set. Both Set and ImmutableSet derive from BaseSet, an abstract class useful for determining whether something is a set: isinstance(obj, BaseSet).
Upvotes: 1
Reputation: 6572
I have no any deep knowledge on them - honestly until I saw your question, I thought that they were identical.
Now checked
>>> from sets import Set
>>> x = Set()
>>> y = set()
>>> len(dir(y))
54
>>> len(dir(x))
63
and realized that they have some dfferences
>>> Y = set(dir(y))
>>> X = set(dir(x))
>>> X-Y
set(['_compute_hash', '__module__', '_update', '_binary_sanity_check', '__setstate__', '__deepcopy__', '_repr', '__as_immutable__', 'union_update', '__slots__', '__copy__', '__as_temporarily_immutable__', '_data', '__getstate__'])
>>> Y-X
set(['__rand__', '__ror__', '__rsub__', '__rxor__', 'isdisjoint'])
Ofcourse this doesn't give any clear information on their differences, but shows that they are not identical :)
Upvotes: 1