Recently I saw several questions about unexpected behavior when performing is
test on the same bound method of the same object. Specifically, please see questions Why does `instance_of_object.foo is instance_of_object.foo` evaluate False? and Two methods inherited from one method in class are different in instances, aren't they?
Let's describe the issue, for example, as described in the first linked question above. Let's assume a class:
class A(object):
def foo(self):
pass
Then the strange thing observed in that question is that if we create an object of type A
such as a = A()
then test a.foo is a.foo
fails (returns False
). According to answer given in https://stackoverflow.com/a/48736729, a new method
instance is created every time a bound method is accessed.
If that is correct and explains why a.foo is a.foo
is always False
, why theid
s of these presumably distinct method
objects are equal:
>>> id(a.foo) == id(a.foo)
True