Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108259
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Stephen Hansen <me@ixokai.io> |
| Newsgroups | comp.lang.python |
| Subject | Re: Why do these statements evaluate the way they do? |
| Date | Sat, 07 May 2016 00:49:29 -0700 |
| Lines | 69 |
| Message-ID | <mailman.449.1462607378.32212.python-list@python.org> (permalink) |
| References | <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> <1462607369.586191.600741793.64CEDF09@webmail.messagingengine.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de r88pCHyIYcBoX7fVfTVDpAlk9lpRv83cy7Q0ZNBQCJbw== |
| Return-Path | <me@ixokai.io> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'true,': 0.04; 'none,': 0.05; 'means,': 0.07; 'integers': 0.09; 'objects.': 0.09; 'received:internal': 0.09; 'statements': 0.09; 'subject:Why': 0.09; 'python': 0.10; 'value.': 0.15; '"are': 0.16; '"is"': 0.16; 'caches': 0.16; 'equal.': 0.16; 'equality.': 0.16; 'integers,': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'mine.': 0.16; 'reason.': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'usually.': 0.16; 'wrote:': 0.16; 'comparing': 0.18; '>>>': 0.20; '(see': 0.20; 'otherwise,': 0.20; 'fairly': 0.22; 'stephen': 0.22; 'trying': 0.22; '(or': 0.23; 'header:In-Reply-To:1': 0.24; 'testing': 0.25; "doesn't": 0.26; 'example': 0.26; 'figure': 0.27; 'compare': 0.27; 'fri,': 0.27; 'not.': 0.27; 'pieces': 0.27; 'said,': 0.27; '"do': 0.29; 'equality': 0.29; 'equivalent,': 0.29; 'objects': 0.29; "i'm": 0.30; 'strongly': 0.30; 'everyone': 0.31; 'useful': 0.33; 'equal': 0.34; 'false': 0.35; 'identity': 0.35; 'mine': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'really': 0.37; 'two': 0.37; 'say': 0.37; 'things': 0.38; 'received:66': 0.38; 'someone': 0.38; 'mean': 0.38; 'why': 0.39; 'does': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'care': 0.60; 'your': 0.60; 'header:Message-Id:1': 0.61; 'course': 0.62; 'above,': 0.63; 'different': 0.63; 'our': 0.64; 'you.': 0.64; "they're": 0.66; 'evaluate': 0.72; 'paper': 0.73; 'hoping': 0.77; "'if": 0.84; 'confusing': 0.84; 'using.': 0.84; 'shares': 0.93; 'hand,': 0.97 |
| DKIM-Signature | v=1; a=rsa-sha1; c=relaxed/relaxed; d=ixokai.io; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=ept/lZTmNYNhIss7lzMWuLUfI/o=; b=OaDOdl AdXTyyrFE7iQoJYPEfT/XvG30Avt31JP5ht+S4MR9j4DG9VdiRfcUwMK0YGEp1iv uSoTx1XXXo+hggadh0DqtLT+A8bLDWEwo0qP1nosUrkcq11A2Pu3KQI4sjTfAZuJ xU6e8rRa3GerXqv8IWIXyk/18UuyNiqRwjl5w= |
| DKIM-Signature | v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=ept/lZTmNYNhIss 7lzMWuLUfI/o=; b=dLUTx7233x7XyLILDMTKPFTLpUxi6DrNdu57UjHR3vs66bC BKBidbh/M6SU3HBaDWG5GzEak4mJPSj4zCWLB1/P5KaZ3E7ZGhlSi6k/r3NG2D7o SJcHq1BSV8Su041zgN9drCbjpONc3ihhIVw5XbcQo4mgVypOzB7urhX5vBzc= |
| X-Sasl-Enc | CjoIfi2NMVzQSZFxPrRoUb8y41XQuVKHHw9RWg0h4gy8 1462607369 |
| X-Mailer | MessagingEngine.com Webmail Interface - ajax-140377c4 |
| In-Reply-To | <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> |
| 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 | <1462607369.586191.600741793.64CEDF09@webmail.messagingengine.com> |
| X-Mailman-Original-References | <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> |
| Xref | csiph.com comp.lang.python:108259 |
Show key headers only | View raw
On Fri, May 6, 2016, at 11:36 PM, Anthony Papillion wrote: > I'm trying to figure out why the following statements evaluate the way > they do and I'm not grasping it for some reason. I'm hoping someone can > help me. > > 40+2 is 42 #evaluates to True > But > 2**32 is 2**32 #evaluates to False > > This is an example taken from a Microsoft blog on the topic. They say the > reason is because the return is based on identity and not value but, to > me, these statements are fairly equal. Yes, those statements are fairly *equal*. But you are not testing *equality*. The "is" operator is testing *object identity*. >>> a = 123456 >>> b = 123456 >>> a is b False >>> a == b True Equality means, "do these objects equal the same", identity means, "are these specific objects the exact same objects". In the above, a and b are different objects. There's two objects that contain a value of 12345. They are equal (see the == test), but they are different objects. If you write on a piece of paper "12345", and I write on a piece of paper "12345", are those two pieces of paper identical? Of course not. Your paper is in your hand, mine is in mine. These are different pieces of paper. Just look at them: they're clearly not the same thing. That said, if you look at the number they contain, they're the same value. Our two pieces of paper are equivalent, but they're different objects. This might get confusing for you because: >>> a = 123 >>> b = 123 >>> a is b True >>> a == b True This happens because Python caches small integers, so everytime you write '123', it re-uses that same '123' object everyone else is using. It shares those objects as an optimization. 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. -- Stephen Hansen m e @ i x o k a i . i o
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Why do these statements evaluate the way they do? Stephen Hansen <me@ixokai.io> - 2016-05-07 00:49 -0700
csiph-web