is operator versus id() function

Discussion in 'Python' started by Candide Dandide, Apr 5, 2013.

  1. 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).
    Candide Dandide, Apr 5, 2013
    #1
    1. Advertising

  2. On 5 April 2013 14:49, Candide Dandide <> 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
    Arnaud Delobelle, Apr 5, 2013
    #2
    1. Advertising

  3. Candide Dandide

    candide Guest

    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.
    candide, Apr 5, 2013
    #3
  4. Candide Dandide

    candide Guest

    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.
    candide, Apr 5, 2013
    #4
  5. Candide Dandide

    Tim Delaney Guest

    On 6 April 2013 03:40, candide <> 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
    Tim Delaney, Apr 5, 2013
    #5
  6. Candide Dandide

    Nobody Guest

    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.
    Nobody, Apr 6, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,873
    Scott M.
    Oct 11, 2003
  2. Russ

    script versus code versus ?

    Russ, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,477
  3. Alf P. Steinbach
    Replies:
    10
    Views:
    3,045
    Alf P. Steinbach
    Jul 27, 2011
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    239
    Christoffer Sawicki
    Sep 2, 2006
  5. Paul Butcher
    Replies:
    12
    Views:
    685
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page