classification
Title: operator "is"
Type: behavior Stage:
Components: Versions: Python 2.4, Python 2.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, georg.brandl, gvanrossum, shigin
Priority: normal Keywords:

Created on 2008-01-31 16:17 by shigin, last changed 2008-02-01 16:04 by gvanrossum. This issue is now closed.

Messages (5)
msg61905 - (view) Author: Alexander Shigin (shigin) Date: 2008-01-31 16:17
Hello,

The operator "is" works strange with methods, i.e.:

>>> a = 1
>>> a.__abs__ is a.__abs__
False

If this is the preferred behavior by some reasons, I think 
documentation should explain it.

It was tested on python 2.4.4, 2.5.1 and trunk r60477.
msg61906 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-31 16:19
Yes, it is expected since the descriptor access to methods creates a new
bound method object every time. The "is" operator does not work special
with methods.

There is another issue about documenting this, so closing as a duplicate.
msg61907 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-01-31 16:24
It's actually documented in
http://docs.python.org/dev/reference/datamodel.html

"""
Note that the transformation from function object to (unbound or bound)
method object happens each time the attribute is retrieved from the
class or instance
"""
msg61908 - (view) Author: Alexander Shigin (shigin) Date: 2008-01-31 16:46
I think it's good idea to make a note in Comparisons section or make 
some reference to data model section.

"""
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.
"""

But,
>>> id(a.__abs__) == id(a.__abs__)
True

And only example below shows me error in my objection.
>>> id(w)
6636344
>>> p = id(w)
>>> del w
>>> e = 1
>>> id(e)
6636344

I'm sorry for spam. I've missed Issue1410739 when search for any open 
bug.
msg61962 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-02-01 16:04
id(a.__abs__) == id(a.__abs__)

has a completely different explanation -- it so happens that the first
instantiation of a.__abs__ is freed after id() is called on it and the
second instantiation happens to reuse that same memory block.
History
Date User Action Args
2008-02-01 16:04:22gvanrossumsetnosy: + gvanrossum
messages: + msg61962
2008-01-31 16:46:29shiginsetmessages: + msg61908
2008-01-31 16:24:28amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg61907
2008-01-31 16:19:03georg.brandlsetstatus: open -> closed
resolution: duplicate
messages: + msg61906
nosy: + georg.brandl
2008-01-31 16:17:01shigincreate