Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42829

Re: is operator versus id() function

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'cpython': 0.05; 'correct.': 0.07; '(currently': 0.09; 'builtin': 0.09; 'function,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '"a"': 0.16; 'a(object):': 0.16; 'address).': 0.16; 'compares': 0.16; 'equivalence': 0.16; 'garbage': 0.16; 'objects;': 0.16; 'subject:versus': 0.16; 'unbound': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'code,': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'integer': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'doc': 0.31; 'slot': 0.31; 'class': 0.32; 'probably': 0.32; 'quite': 0.32; 'says': 0.33; 'implemented': 0.33; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'books,': 0.36; 'false': 0.36; 'representing': 0.36; 'method': 0.36; 'so,': 0.37; 'two': 0.37; 'fact': 0.38; 'explain': 0.39; 'does': 0.39; 'reported': 0.39; 'sure': 0.39; 'new': 0.61; "you've": 0.63; 'more': 0.64; 'acts': 0.74; 'right!': 0.84; 'victim': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:content-transfer-encoding; bh=dN7sgfOr4nRHNLHWSudFjpSY/77S+5KAPySDweMDLy8=; b=CQylLsWeDDiYQzQjRH+n8Rsf0fXFVnGl0fbSyn50y4es8RHsHXTnLqRVy+ND/eaXXY i0kMNCdY6IGzcf29IWJTtONgsWCAbOLjj8Ta1kC6VWUIg0wwHJ9Ul789eJs1+IZ7TVUe /sJhXqVE+WryR4h4bA3U1x2MIrILwOz5TCeIHDhrYjvXqG/mE6ZylGoPy7oY4UclmkVv RVboS2Jb/5GSIFrUjgYE5YqXGRwDeCaZVdqKh9PMNHuXL+1F8zj9VpGu+A1GHtgG07bt 5rCg82l8teP1DFYOwZLjZWvBS59dY0bA/T89j/5XSC50TFA1hDom/q+ZLmr8K5aCuHrw o7Ig==
MIME-Version 1.0
X-Received by 10.15.36.135 with SMTP id i7mr20291420eev.34.1365173635742; Fri, 05 Apr 2013 07:53:55 -0700 (PDT)
In-Reply-To <ed17e4ac-2000-42ef-aa2b-69ff421ae3fa@googlegroups.com>
References <ed17e4ac-2000-42ef-aa2b-69ff421ae3fa@googlegroups.com>
Date Fri, 5 Apr 2013 15:53:55 +0100
Subject Re: is operator versus id() function
From Arnaud Delobelle <arnodel@gmail.com>
To Candide Dandide <c.candide@laposte.net>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.144.1365173642.3114.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365173643 news.xs4all.nl 6878 [2001:888:2000:d::a6]:45653
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42829

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web