Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a method not identical to itself?

Tags:

The Python documentation about the is operator says:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

Let's try that:

>>> def m():
...   pass
... 
>>> m is m
True

The Python documentation also says:

Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info.

>>> class C:
...   def m():
...     pass
... 
>>> C.m is C.m
False

I searched for more explanations, but I was not able to find any.

Why is C.m is C.m false?

I am using Python 2.x. As noted in the answers below, in Python 3.x C.m is C.m is true.