Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'win32': 0.03; 'cpython': 0.05; 'method.': 0.07; 'reason,': 0.07; 'bug.': 0.09; 'instances.': 0.09; 'method,': 0.09; 'snippet': 0.09; 'python': 0.11; 'def': 0.12; 'a(object):': 0.16; 'subject:versus': 0.16; 'unbound': 0.16; 'unexpected': 0.16; '\xc3\xa9crit\xc2\xa0:': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'value.': 0.19; '>>>': 0.22; '>>>': 0.24; 'integer': 0.24; 'fairly': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'tim': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; 'constant': 0.31; 'doc': 0.31; 'object.': 0.31; 'sep': 0.31; 'class': 0.32; 'url:python': 0.33; 'could': 0.34; 'created': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'accessing': 0.36; 'false': 0.36; 'method': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'two': 0.37; 'skip:& 10': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'explain': 0.39; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'read': 0.60; 'url:3': 0.61; "you've": 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'answer.': 0.68; 'guaranteed': 0.75; 'accessed.': 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:content-type; bh=UX/hzL3vopC8dNbnJlCpoW1Guh1FRJ1dB/PnSWrxVj8=; b=lVb69ygEHCS/lex+m6bY7IPZRaSUSX3AB3UdseYjqSuewilsKe3f9M6oDRbZrtq5YQ Z4OVA/eHuTjOAfHYF6717p9TKt4tdLITnAhRIFzPPEjfgkn7iZWmjm9m3gynFAF6VeKs 9Gk8wbcnSgHwriz9JyKB3n6LjHV8S2ASDwGu4/b7mrr/ICqsxmUus/mQCpQm5QDfBCn7 fw4ggzKN6QhEgP6f82jxBMx/V8PgJbQXWYO/8+HXWF/PiBw2vZVXoob9VDtgOzF8Mwmi PWgygYYymdVnYYnl9E4UoMfuEVI4D3qt5v76o56ANPc3ADxctWwvWluVWYgGD/Shpovs Y5lA== MIME-Version: 1.0 X-Received: by 10.60.37.68 with SMTP id w4mr9088150oej.62.1365191821362; Fri, 05 Apr 2013 12:57:01 -0700 (PDT) In-Reply-To: References: Date: Sat, 6 Apr 2013 06:57:01 +1100 Subject: Re: is operator versus id() function From: Tim Delaney To: Python Content-Type: multipart/alternative; boundary=089e0111c130900ef404d9a27d0b 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: 100 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365191830 news.xs4all.nl 6860 [2001:888:2000:d::a6]:35546 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42848 --089e0111c130900ef404d9a27d0b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 6 April 2013 03:40, candide wrote: > Le vendredi 5 avril 2013 16:53:55 UTC+2, Arnaud Delobelle a =C3=A9crit : > > > > > > 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=3DA() >>> print(id(a.f) =3D=3D id(a.f), a.f is a.f) True False >>> Tim Delaney --089e0111c130900ef404d9a27d0b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
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 =C3=A9crit=C2= =A0:


>
> You've fallen victim to the fact that CPython is very quick to col= lect
>
> 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() <<= a href=3D"http://docs.python.org/3.3/library/functions.html#id">http://docs= .python.org/3.3/library/functions.html#id>, you will see that it say= s:

Return the "identity" of an objec= t. 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()=C2=A0valu= e.

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 cre= ated when accessed. So A.f and a.f are not the same object - one is a funct= ion (an unbound method, but there's no distinction in Python 3.x) and t= he other is a bound method. For that reason, accessing a.f twice will retur= n two different bound method instances.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2= 012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or &quo= t;license" for more information.
>>> class A(object):
... =C2=A0 =C2=A0 def f(self):=
... =C2=A0 =C2=A0 =C2=A0 =C2=A0 print("A")
.= ..
>>> a=3DA()
>>> print(id(a.f) =3D= =3D id(a.f), a.f is a.f)
True False
>>>

Tim = Delaney
--089e0111c130900ef404d9a27d0b--