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


Groups > comp.lang.python > #108261

Re: Why do these statements evaluate the way they do?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Why do these statements evaluate the way they do?
Date Sat, 7 May 2016 18:18:07 +1000
Lines 42
Message-ID <mailman.451.1462609090.32212.python-list@python.org> (permalink)
References <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> <1462607369.586191.600741793.64CEDF09@webmail.messagingengine.com> <CAPTjJmqcY3LFFrBwagG=JB1HJ4Up4EVibysKNVzxnos5xqymfg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 9EXzkgDp7QK+Z1KIUqTDVQsU/VoSR4xbP3oxiYBeDvsA==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'anyway.': 0.04; 'true,': 0.04; 'none,': 0.05; 'cc:addr:python-list': 0.09; 'integers': 0.09; 'objects.': 0.09; 'subject:Why': 0.09; 'thread': 0.10; 'question.': 0.13; 'def': 0.13; 'argument': 0.15; '"is': 0.16; '"some': 0.16; "(it's": 0.16; '2016': 0.16; 'equality.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'object()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'unequal,': 0.16; 'used:': 0.16; 'usually.': 0.16; 'whatsoever': 0.16; 'wrote:': 0.16; 'comparing': 0.18; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'otherwise,': 0.20; 'stephen': 0.22; '(or': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'compare': 0.27; 'message-id:@mail.gmail.com': 0.27; 'equality': 0.29; 'strings,': 0.29; 'thread,': 0.29; 'raise': 0.29; 'strongly': 0.30; 'useful': 0.33; 'safely': 0.33; 'equal': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'false': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'faster': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'things': 0.38; 'difference': 0.38; 'received:209': 0.38; 'mean': 0.38; 'does': 0.39; 'subject:the': 0.39; 'where': 0.40; 'care': 0.60; 'default': 0.61; 'back': 0.62; 'skip:n 10': 0.62; 'different': 0.63; 'safe': 0.63; 'times': 0.63; 'you.': 0.64; 'between': 0.65; "they're": 0.66; 'funny': 0.83; "'if": 0.84; 'chrisa': 0.84; 'distinguish': 0.84; 'idiom': 0.84; 'to:none': 0.91; 'checks.': 0.91; 'gender': 0.91; 'safer': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=KoOhj4DRa/Z3E45UQsSidkm2kkBkQBYh4h4tCvKE44g=; b=VHrIxZyEX4HDrVOBQOBIaElqLbw/dxuiADkmcwBEe2bje/rbgqNikAKl2MMt2UMomr PeMtNeM5r8A1SGASREaHatEgeQTUVHiuaJ33n5no8wEOdSe+njC4CLpoorsT7msjklWj vzG7Qtf8n3dVoTuxZ/goyHUedKxwFuCEnnrsDSBb/n3e6GNaoaLXZqgErq3jsUWma3NF ReS36ISjHyhdU+VSb7X3aqhpES1wTR7kpcl/MwhYPaENiRLDxmXDKzod6xRjOGxaYdhc 6sXJE6Lu4U7eST7uh8msfoVrstFVT7tXKN7Qi75rgzfEFphojZYlUV2SiFwFMb97sZmz 3u0g==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=KoOhj4DRa/Z3E45UQsSidkm2kkBkQBYh4h4tCvKE44g=; b=EPhzEv2emaPJe6hhhKbJm9zPreCDOACqRB8PfPLZusSqB20cItjpT1Zz8Ro7KYggWL RL62VW2bJ2cpACSUyYJkrXUYiZjwzW84m2lvyI+rrZQdY9zqQbBa67+3v9VF3FkXncXx elgY/WPArj+UQvFFIHy6PN8tCY/7rXQVwyEvZq088csFMCvJ0zQyZYYockm9qjexprmg ZDocAK4M86qQaYSSYUre9ae/VG0953rirIg3ZnpSo3/CzGF+lI3EYfgaC5xsZ41X2Kvj 4ySSrIXB6oNx++qLqTR7Vojho8rGTVAr5hfiPWiuQ9qf08bmvSpzu/tf1Hn5iOiUGT8O +Sfw==
X-Gm-Message-State AOPr4FVgvW+cshMyJS0ymW6HeV/GvIVrBEoPiggAUtRqK6CEw81dYoy4wxqiiPB5fsfAn9fHN6bCvLfCDR94gw==
X-Received by 10.112.52.196 with SMTP id v4mr9940445lbo.59.1462609087838; Sat, 07 May 2016 01:18:07 -0700 (PDT)
In-Reply-To <1462607369.586191.600741793.64CEDF09@webmail.messagingengine.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CAPTjJmqcY3LFFrBwagG=JB1HJ4Up4EVibysKNVzxnos5xqymfg@mail.gmail.com>
X-Mailman-Original-References <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> <1462607369.586191.600741793.64CEDF09@webmail.messagingengine.com>
Xref csiph.com comp.lang.python:108261

Show key headers only | View raw


On Sat, May 7, 2016 at 5:49 PM, Stephen Hansen <me@ixokai.io> wrote:
> The long and short of it is: you should almost never use 'is' for
> comparing integers (or strings). It doesn't mean what you think it does
> and isn't useful to you. Compare equality.
>
> In general, the only things you should use 'is' for is when comparing to
> singletons like None, True or False (And consider strongly not comparing
> against False/True with is, but instead just 'if thing' and if its True,
> it passes).
>
> Otherwise, 'is' should only be used when you're comparing *object
> identity*. You don't need to do that usually. Only do it when it matters
> to you that an object with value A might be equal to an object of value
> B, but you care that they're really different objects.

Equality sometimes is immaterial; there are times when you want to
know whether this object you got back is the same one that was sent
in, and equality's out of the question. (It's funny how there's this
parallel thread about "equal opportunity", in which gender equality is
the focus. In this thread, equality really CAN be irrelevant.) For
example, to adequately distinguish between "argument omitted" and
"some argument was provided", this idiom is commonly used:

_SENTINEL = object()
def next(iterator, default=_SENTINEL):
    try:
        return iterator.__next__()
    except StopIteration:
        if default is _SENTINEL: raise
        return default

It makes no difference whatsoever whether the default might compare
equal to something; all that matters is: "Is this the sentinel?". And
this check is faster and safer than equality, anyway.

But with integers and strings, always do equality checks. (Interned
strings can safely be identity-checked, which would be faster in the
case where they're unequal, but that's a micro-optimization that's
safe ONLY if both strings have definitely been interned, so it's
better not to bother.)

ChrisA

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


Thread

Re: Why do these statements evaluate the way they do? Chris Angelico <rosuav@gmail.com> - 2016-05-07 18:18 +1000

csiph-web