所以在Python 3中,我可以使用object().__eq__
。我目前使用它作爲可映射函數,相當於lambda x: x is object()
。Python 2 vs Python 3雙下劃線方法
我正在使用它作爲定點(因爲None
將有不同的含義從沒有參數)。
>>> import sys
>>> print(sys.version)
3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
>>> object.__eq__
<slot wrapper '__eq__' of 'object' objects>
>>> object().__eq__
<method-wrapper '__eq__' of object object at 0x000002CC4E569120>
但是在Python 2,這不起作用:
>>> import sys
>>> print sys.version
2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)]
>>> object.__eq__
<method-wrapper '__eq__' of type object at 0x0000000054AA35C0>
>>> object().__eq__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute '__eq__'
>>> dir(object)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
爲什麼沒有這個功能存在?我怎麼能效仿(performantly與Python 2兼容)
$ python3 -m timeit "sentinel = object(); tests = [sentinel] * 100 + [None] * 100" "list(filter(sentinel.__eq__, tests))"
100000 loops, best of 3: 8.8 usec per loop
$ python3 -m timeit "sentinel = object(); tests = [sentinel] * 100 + [None] * 100; exec('def is_sentinel(x): return sentinel is x', locals(), globals())" "list(filter(is_sentinel, tests))"
10000 loops, best of 3: 29.1 usec per loop
這正是您不應該使用像這樣的'__dunder__'方法的原因。使用'import operator; operator.eq' –
或者實際上使用'lambda thing:thing is sentinel';平等並不一定是身份。 – jonrsharpe
@jonrsharpe我使用字面上的s = object()'。爲此,我認爲平等被定義爲身份。 – Artyer