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


Groups > comp.lang.python > #79735

Re: id == vs is

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
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; 'cpython': 0.05; 'assign': 0.07; 'expressions': 0.07; 'dan': 0.09; 'expression:': 0.09; 'happen.': 0.09; 'anyway': 0.14; '"is"': 0.16; '"x"': 0.16; 'garbage': 0.16; 'to:name:python list': 0.16; 'exception': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'points': 0.29; 'message- id:@mail.gmail.com': 0.30; '(although': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'two': 0.37; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'kind': 0.63; 'real': 0.63; 'here': 0.66; 'obvious': 0.74; 'safe.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=+Cs1raYMOXqHTVPR0mbW7vSRpVO2z5CfieZptJ6DZqA=; b=dv9EYsof93Zchtroodqp08Lsm0FMATgkHYty/ueZ0wiY7J6lP6NsNj3gxL4ylV9iqn UqiamakKpkubqXgeqfFSP8iltjuMlGuR7EK1LCDa/rpnYO8fjJhwDyLIdg69n7/VbVcP mXGkkCGtm+DgDRn/bsKrsE0xl13AS+t4iZsvamGQXw2HZlgBqlceOJcbXTylmeI9S0Ni O2C6vGEEtthxOCMPKmZT+5xWgMUW+le2IpwuEo9NDhkloEl6c2AwcY2rMNoqjuMHQafN Ll7RmG9uiMHCNHc63u/6GnaNirikNc71jnIHz5DgLi6WLMNaDA8CGCFJkeZK8s681Kmf kzMQ==
X-Received by 10.112.182.1 with SMTP id ea1mr19530889lbc.16.1414369241424; Sun, 26 Oct 2014 17:20:41 -0700 (PDT)
MIME-Version 1.0
Sender joshua.landau.ws@gmail.com
In-Reply-To <CAGGBd_qwcZuReU6rY9bcAa-xH2Kc25dCjubF2SsjppxP3L3J9Q@mail.gmail.com>
References <CAGGBd_qwcZuReU6rY9bcAa-xH2Kc25dCjubF2SsjppxP3L3J9Q@mail.gmail.com>
From Joshua Landau <joshua@landau.ws>
Date Mon, 27 Oct 2014 00:20:01 +0000
X-Google-Sender-Auth jsM2vJUh3e4c5woSUT3HHTbagIA
Subject Re: id == vs is
To Python List <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15208.1414369249.18130.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414369249 news.xs4all.nl 2854 [2001:888:2000:d::a6]:39206
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79735

Show key headers only | View raw


On 27 October 2014 00:12, Dan Stromberg <drsalists@gmail.com> wrote:
> Are the following two expressions the same?
>
> x is y
>
> Id(x) == id(y)

Much of the time, but not all the time. The obvious exception is if
"id" is redefined, but that one's kind of boring. The real thing to
watch out for is if the object that "x" points to is garbage collected
before "y" is evaluated:

   nothing = "!"

    id("hello" + nothing) == id("hello" + nothing)
    #>>> True

    ("hello" + nothing) is ("hello" + nothing)
    #>>> False

Since in the first case the ("hello" + nothing) gets garbage
collected, CPython is allowed to re-use its id. If instead you assign
them outside of the expression:

    nothing = "!"

    x = "hello" + nothing
    y = "hello" + nothing
    id(x) == id(y)
    #>>> False

the collection cannot happen.

Note that in this case CPython is allowed to deduplicate these strings
anyway (although in this case it does not), so using "is" here is not
safe.

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


Thread

Re: id == vs is Joshua Landau <joshua@landau.ws> - 2014-10-27 00:20 +0000

csiph-web