is operator versus id() function

Tim Delaney timothy.c.delaney at gmail.com
Fri Apr 5 15:57:01 EDT 2013


On 6 April 2013 03:40, candide <c.candide at laposte.net> wrote:

> Le vendredi 5 avril 2013 16:53:55 UTC+2, Arnaud Delobelle a écrit :
>
>
> >
> > You've fallen victim to the fact that CPython is very quick to collect
> >
> > garbage.
>
>
> OK, I get it but it's a fairly unexpected behavior.
> Thanks for the demonstrative snippet of code and the instructive answer.
>

If you read the docs for id() <
http://docs.python.org/3.3/library/functions.html#id>, you will see that it
says:

Return the "identity" of an object. This is an 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.

If you think it could explain things better, please submit a doc bug.

I think part of your confusion here is that bound methods in Python are
created when accessed. So A.f and a.f are not the same object - one is a
function (an unbound method, but there's no distinction in Python 3.x) and
the other is a bound method. For that reason, accessing a.f twice will
return two different bound method instances.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
...     def f(self):
...         print("A")
...
>>> a=A()
>>> print(id(a.f) == id(a.f), a.f is a.f)
True False
>>>

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130406/d32b2ebe/attachment.html>


More information about the Python-list mailing list