The Python documentation for the id function states:
Return the "identity" of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
(emphasis mine)
When you do id(Hello.hello) == id(Hello.hello)
, the method object is created only briefly and is considered "dead" after the first call to 'id'. Because of the call to id
, you only need Hello.hello
to be alive for a short period of time -- enough to obtain the id. Once you get that id, the object is dead and the second Hello.hello
can reuse that address, which makes it appear as if the two objects have the same id.
This is in contrast to doing Hello.hello is Hello.hello
-- both instances have to live long enough to be compared to each other, so you end up having two live instances.
If you instead tried:
>>> a = Hello.hello
>>> b = Hello.hello
>>> id(a) == id(b)
False
...you'd get the expected value of False
.