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


Groups > comp.lang.python > #108257

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Kev Dwyer <kevin.p.dwyer@gmail.com>
Newsgroups comp.lang.python
Subject Re: Why do these statements evaluate the way they do?
Date Sat, 07 May 2016 08:04:04 +0100
Lines 49
Message-ID <mailman.448.1462604660.32212.python-list@python.org> (permalink)
References <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> <ngk416$uol$1@ger.gmane.org>
Reply-To kevin.p.dwyer@gmail.com
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de xVdFyv1xEXopuxAfYetykgwzlaj4mL2ia8d0oKgjwDcQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'cpython': 0.05; 'revision': 0.05; 'false.': 0.07; '*is*': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'subject:Why': 0.09; 'python': 0.10; 'caches': 0.16; 'equal.': 0.16; 'range,': 0.16; 'reason.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'tests': 0.18; '>>>': 0.20; 'fairly': 0.22; 'object.': 0.22; 'trying': 0.22; 'dec': 0.23; 'header:User- Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'linux': 0.26; 'figure': 0.27; 'object,': 0.27; 'objects': 0.29; "i'm": 0.30; 'skip:[ 10': 0.31; 'int': 0.33; 'true.': 0.33; 'false': 0.35; 'identity': 0.35; 'but': 0.36; 'created': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:org': 0.37; 'someone': 0.38; 'why': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'side': 0.62; 'more': 0.63; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'evaluate': 0.72; 'reply- to:addr:gmail.com': 0.76; 'hoping': 0.77; '3.5.1': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host cpc93820-hari18-2-0-cust1319.20-2.cable.virginm.net
Mail-Copies-To kevin.p.dwyer@gmail.com
User-Agent KNode/4.14.10
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 <ngk416$uol$1@ger.gmane.org>
X-Mailman-Original-References <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org>
Xref csiph.com comp.lang.python:108257

Show key headers only | View raw


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.
> 
> Can someone clue me in?
> 
> Anthony

The *is* operator tests for identity, that is whether the objects on either 
side of the operator are the same object.

CPython caches ints in the range -5 to 256 as an optimisation, so ints in 
this range are always the same object, and so the is operator returns True.

Outside this range, a new int is created as required, and comparisons using 
is return False.

This can be seen by looking at the id of the ints:

Python 3.5.1 (default, Dec 29 2015, 10:53:52)                                                                                               
[GCC 4.8.3 20140627 [gcc-4_8-branch revision 212064]] on linux                                                                              
Type "help", "copyright", "credits" or "license" for more information. 
>>> a = 42
>>> b = 42
>>> a is b
True
>>> id(a)
9186720
>>> id(b)
9186720
>>> c = 2 ** 32
>>> d = 2 ** 32
>>> c is d
False
>>> id(c)
140483107705136
>>> id(d)
140483107705168

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


Thread

Re: Why do these statements evaluate the way they do? Kev Dwyer <kevin.p.dwyer@gmail.com> - 2016-05-07 08:04 +0100

csiph-web