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: 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: References: Date: Fri, 5 Apr 2013 15:53:55 +0100 Subject: Re: is operator versus id() function From: Arnaud Delobelle To: Candide Dandide Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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 equiva= lent to id(o1) =3D=3D id(o2). This equivalence is reported in many books, f= or 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=3DA() > print id(A.f) =3D=3D id(a.f), A.f is a.f > #-------------------------------------------------------- > > > outputing: > > True False > > So, could someone please explain what exactly the is operator returns ? T= he official doc says : > > The =E2=80=98is=E2=80=98 operator compares the identity of two objects; t= he id() function returns an integer representing its identity (currently im= plemented as its address). And the doc is right! >>> Af =3D A.f >>> af =3D a.f >>> print id(Af) =3D=3D 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) =3D=3D 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 ... --=20 Arnaud