Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > is operator versus id() function

Reply
Thread Tools

is operator versus id() function

 
 
Candide Dandide
Guest
Posts: n/a
 
      04-05-2013
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, forinstance 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 ? Theofficial 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).
 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      04-05-2013
On 5 April 2013 14:49, Candide Dandide <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
candide
Guest
Posts: n/a
 
      04-05-2013
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.
 
Reply With Quote
 
candide
Guest
Posts: n/a
 
      04-05-2013
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.
 
Reply With Quote
 
Tim Delaney
Guest
Posts: n/a
 
      04-05-2013
On 6 April 2013 03:40, candide <(E-Mail Removed)> 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

 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      04-06-2013
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
function Object,function statement,function operator alex Javascript 3 12-28-2006 02:57 PM
Operator versus Function Object cainiaodelixiang@gmail.com C++ 3 08-02-2006 03:26 AM



Advertisments