The results from the code below in Python 2.7 struck me as a contradiction. The is
operator is supposed to work with object identity and so is id
. But their results diverge when I'm looking at a user-defined method. Why is that?
py-mach >>class Hello(object):
... def hello():
... pass
...
py-mach >>Hello.hello is Hello.hello
False
py-mach >>id(Hello.hello) - id(Hello.hello)
0
I found the following excerpt from the description of the Python data model somewhat useful. But it didn't really make everything clear. Why does the id
function return the same integer if the user-defined method objects are constructed anew each time?
User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method object. When the attribute is a user-defined method object, a new method object is only created if the class from which it is being retrieved is the same as, or a derived class of, the class stored in the original method object; otherwise, the original method object is used as it is.