Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42823 > unrolled thread
| Started by | Candide Dandide <c.candide@laposte.net> |
|---|---|
| First post | 2013-04-05 06:49 -0700 |
| Last post | 2013-04-06 14:35 +0100 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
is operator versus id() function Candide Dandide <c.candide@laposte.net> - 2013-04-05 06:49 -0700
Re: is operator versus id() function Arnaud Delobelle <arnodel@gmail.com> - 2013-04-05 15:53 +0100
Re: is operator versus id() function candide <c.candide@laposte.net> - 2013-04-05 09:40 -0700
Re: is operator versus id() function Tim Delaney <timothy.c.delaney@gmail.com> - 2013-04-06 06:57 +1100
Re: is operator versus id() function candide <c.candide@laposte.net> - 2013-04-05 09:40 -0700
Re: is operator versus id() function Nobody <nobody@nowhere.com> - 2013-04-06 14:35 +0100
| From | Candide Dandide <c.candide@laposte.net> |
|---|---|
| Date | 2013-04-05 06:49 -0700 |
| Subject | is operator versus id() function |
| Message-ID | <ed17e4ac-2000-42ef-aa2b-69ff421ae3fa@googlegroups.com> |
Until now, I was quite sure that the is operator acts the same as the id builtin function, or, to be more formal, that o1 is o2 to be exactly equivalent to id(o1) == id(o2). This equivalence is reported in many books, for instance Martelli's Python in a Nutshell.
But with the following code, I'm not still sure the equivalence above is correct. Here's the code :
#--------------------------------------------------------
class A(object):
def f(self):
print "A"
a=A()
print id(A.f) == id(a.f), A.f is a.f
#--------------------------------------------------------
outputing:
True False
So, could someone please explain what exactly the is operator returns ? The official doc says :
The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity (currently implemented as its address).
[toc] | [next] | [standalone]
| From | Arnaud Delobelle <arnodel@gmail.com> |
|---|---|
| Date | 2013-04-05 15:53 +0100 |
| Message-ID | <mailman.144.1365173642.3114.python-list@python.org> |
| In reply to | #42823 |
On 5 April 2013 14:49, Candide Dandide <c.candide@laposte.net> wrote: > Until now, I was quite sure that the is operator acts the same as the id builtin function, or, to be more formal, that o1 is o2 to be exactly equivalent to id(o1) == id(o2). This equivalence is reported in many books, for instance Martelli's Python in a Nutshell. > > But with the following code, I'm not still sure the equivalence above is correct. Here's the code : > > > #-------------------------------------------------------- > class A(object): > def f(self): > print "A" > > a=A() > print id(A.f) == id(a.f), A.f is a.f > #-------------------------------------------------------- > > > outputing: > > True False > > So, could someone please explain what exactly the is operator returns ? The official doc says : > > The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity (currently implemented as its address). And the doc is right! >>> Af = A.f >>> af = a.f >>> print id(Af) == id(af), Af is af False False You've fallen victim to the fact that CPython is very quick to collect garbage. More precisely, when Python interprets `id(A.f) == id(a.f)`, it does the following: 1. Create a new unbound method (A.f) 2. Calculate its id 3. Now the refcount of A.f is down to 0, so it's garbage collected 4 Create a new bound method (a.f) **and very probably use the same memory slot as that of A.f** 5 Calculate its id 6 ... -- Arnaud
[toc] | [prev] | [next] | [standalone]
| From | candide <c.candide@laposte.net> |
|---|---|
| Date | 2013-04-05 09:40 -0700 |
| Message-ID | <e120252a-22c1-4e4c-9e87-42974a6082ad@googlegroups.com> |
| In reply to | #42829 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Delaney <timothy.c.delaney@gmail.com> |
|---|---|
| Date | 2013-04-06 06:57 +1100 |
| Message-ID | <mailman.160.1365191829.3114.python-list@python.org> |
| In reply to | #42839 |
[Multipart message — attachments visible in raw view] — view raw
On 6 April 2013 03:40, candide <c.candide@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
[toc] | [prev] | [next] | [standalone]
| From | candide <c.candide@laposte.net> |
|---|---|
| Date | 2013-04-05 09:40 -0700 |
| Message-ID | <mailman.150.1365180026.3114.python-list@python.org> |
| In reply to | #42829 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2013-04-06 14:35 +0100 |
| Message-ID | <pan.2013.04.06.13.35.03.48000@nowhere.com> |
| In reply to | #42823 |
On Fri, 05 Apr 2013 06:49:14 -0700, Candide Dandide wrote: > So, could someone please explain what exactly the is operator returns ? > The official doc says : > > The ‘is‘ operator compares the identity of two objects; the id() > function returns an integer representing its identity (currently > implemented as its address). The docs are correct. But an identity is only unique for the lifetime of the object, so "x is y" and "id(x)==id(y)" are only equivalent if the lifetimes of x and y overlap. If the objects have disjoint lifetimes (i.e. one is created after the other has been destroyed), then it's possible for id() to return the same value for both objects, so id(x)==id(y) can return a "false positive" result, as happened in your example.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web