的Martijn是正确的,新的方法是通过.__get__
产生的对象,使他们的地址指针不以等号is
评价。请注意,使用==
将按照Python 2.7中的预期进行评估。然而
Python2.7
class Test(object):
def tmethod(self):
pass
>>> Test.meth is Test.meth
False
>>> Test.meth == Test.meth
True
>>> t = Test()
>>> t.meth is t.meth
False
>>> t.meth == t.meth
True
注意,从一个实例引用的方法不等同于那些从类,因为从一个实例方法一起进行自我参考引用。
>>> t = Test()
>>> t.meth is Test.meth
False
>>> t.meth == Test.meth
False
在Python 3.3 is
运营商的方法更经常的行为一样==
让您得到预期的行为,而不是在这个例子中。这是由于__cmp__
消失以及Python 3中更清晰的方法对象表示造成的;方法现在有__eq__
,并且引用不是动态内建的对象,因此,如果没有Python 2期望,行为可能会如预期的那样。
Python3.3
>>> Test.meth is Test.meth
True
>>> Test.meth == Test.meth
True
>>> Test.meth.__eq__(Test.meth)
True
这适用于Python 3 btw。 – poke