This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 'is' does not work on methods
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: mwh Nosy List: collinwinter, mwh
Priority: normal Keywords:

Created on 2006-01-09 10:04 by collinwinter, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg27258 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2006-01-09 10:04
As of Python 2.4.2 (gcc 3.3.6, linux 2.6.13), the 'is'
comparison operator does not work on instancemethods or
classmethods (though it does work on staticmethods).
For example:

"""
>>> class A(object):
...   def foo(self): pass
...
>>> id(A.foo) == id(A.foo)
True
>>> A.foo is A.foo
False
>>>
>>> a = A()
>>> id(a.foo) == id(a.foo)
True
>>> a.foo is a.foo
False
"""

This example is applicable to old-style classes and
new-style classes, though things get more complicated
in the case of built-in types:

"""
>>> d = dict()
>>> id(d.update) == id(d.update)
True
>>> d.update is d.update
False
>>>
>>> id(dict.update) == id(dict.update)
True
>>> dict.update is dict.update
True
"""

However, using the classmethod 'fromkeys':

"""
>>> d = dict()
>>> id(d.fromkeys) == id(d.fromkeys)
True
>>> d.fromkeys is d.fromkeys
False
>>>
>>> id(dict.fromkeys) == id(dict.fromkeys)
True
>>> dict.fromkeys is dict.fromkeys
False
"""
msg27259 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2006-01-09 11:39
Logged In: YES 
user_id=6656

'is' is working just fine here.  When you execute 'a.foo' a fresh bound method 
object is made, so two executions lead to two different objects.  The thing 
that probably confused you was this:

>>> id(a.foo) == id(a.foo)
True

What happens here is that you execute 'a.foo' which returns a bound method 
object, you call id() on it, the bound method gets deleted, you execute 'a.foo' 
again and call id() on the new bound method object -- which happens to be 
allocated at the same place in memory as the previous one, so the id()s are 
the same.

Closing.
History
Date User Action Args
2022-04-11 14:56:14adminsetgithub: 42778
2006-01-09 10:04:20collinwintercreate