[issue39997] "is" operator doesn't work on method returned from method descriptor

Christian Heimes report at bugs.python.org
Tue Mar 17 16:56:35 EDT 2020


Christian Heimes <lists at cheimes.de> added the comment:

This is not a bug. The "is" operator works as expected. A method descriptor returns a new wrapper object on each access.

CPython uses free lists to "recycle" memory locations to increase performance. id(Class.method.__get__(None, Class)) == id(Class.method) is true because the return value of "Class.method.__get__(None, Class)" is garbage collected and the memory address is reused.

See:

>>> class Class:
...     def method(self): ...
... 
>>> instance = Class()
>>> m1 = Class.method.__get__(instance, Class)
>>> m2 = instance.method
>>> id(m1) == id(m2)
False

----------
nosy: +christian.heimes
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39997>
_______________________________________


More information about the Python-bugs-list mailing list