简体   繁体   English

Set和set之间有区别吗?

[英]Is there a difference between Set and set?

I am using Python 2.7, and just wondering if there is any difference between set() and Set() (ie with/without the capitalization). 我正在使用python 2.7,并且只是想知道set()Set()之间是否有任何区别(即,有无大写)。

Specifically, the Python instructions https://docs.python.org/2/library/sets.html suggest that Sets should be imported and initialized as: 具体来说,Python指令https://docs.python.org/2/library/sets.html建议将Set导入并初始化为:

from sets import Set
x = Set()

I have just been using the command set() without importing anything, ie: 我刚使用过set()命令,但未导入任何内容,即:

x = set()

Just wondering if these are identical, or if they are somehow different. 只是想知道它们是否相同,或者是否有所不同。

As it says in Python documentation the Set class provides every set method except for __hash__() . 就像在Python文档中所说的那样, Set类提供了除__hash__()之外的每个set方法。

For advanced applications requiring a hash method, the ImmutableSet class adds a hash () method but omits methods which alter the contents of the set. 对于需要哈希方法的高级应用程序,ImmutableSet类添加了hash ()方法,但是省略了会更改集合内容的方法。 Both Set and ImmutableSet derive from BaseSet, an abstract class useful for determining whether something is a set: isinstance(obj, BaseSet). Set和ImmutableSet都从BaseSet派生,BaseSet是一个抽象类,可用于确定某物是否为集合:isinstance(obj,BaseSet)。

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 :) 当然,这并不能给出有关它们之间差异的任何清晰信息,但是表明它们并不相同:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM