Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Why do these statements evaluate the way they do? Date: Sat, 7 May 2016 16:48:22 +1000 Lines: 21 Message-ID: References: <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de EYgiHT770hzoM+p5zV506QVIXOxxPoBG+chQCbYKiGOw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'cached': 0.09; 'integers': 0.09; 'statements': 0.09; 'subject:Why': 0.09; 'interpreter': 0.15; '*any*': 0.16; '2016': 0.16; 'cc:name:python list': 0.16; 'equal.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'in- memory': 0.16; 'range,': 0.16; 'reason.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'varies': 0.16; 'wrote:': 0.16; 'integer': 0.18; "shouldn't": 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'machine': 0.21; 'fairly': 0.22; 'object.': 0.22; 'trying': 0.22; 'sat,': 0.23; 'header:In-Reply- To:1': 0.24; 'all.': 0.24; 'example': 0.26; 'figure': 0.27; 'message-id:@mail.gmail.com': 0.27; 'values': 0.28; 'actual': 0.28; 'equality': 0.29; 'objects': 0.29; "i'm": 0.30; 'compared': 0.30; 'received:google.com': 0.35; 'false': 0.35; 'identity': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'version': 0.38; 'received:209': 0.38; 'anything': 0.38; 'someone': 0.38; 'why': 0.39; 'subject:the': 0.39; 'some': 0.40; "you'll": 0.61; 'within': 0.64; 'evaluate': 0.72; 'directly.': 0.76; 'hoping': 0.77; 'chrisa': 0.84; 'to:none': 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=TsjTBjDSxbsIZa3cyM3cuBumFZ+sSidPsK06VPX52sg=; b=q6rDBEuEbldqdVG8DIjTnIfTgspTyLNVw6zUN+D89WBIeBgGk+BJSKazjD1OqaoZBZ qc/li6jQdwVDv7e3dhHcY0KHUAbsq372JxwoKJtfZnhgdRJya353v+39ZmlV+DpjUAM/ NnBkScc48nyQw7088/dhaHXn79pNn9CJrQankx1Po6y6ay6I1m90SBeSRqNJ1LZFso07 ufgepDxh3CHmPm1JciOZx0RcXqUW9iow+agRaMJoxtvXPTj/qphWemcHvJoU2REuhLQO QI0d5A/3IU8DiHZKjPJiKLgNxfNaGY+nq6syNYggqotlwPGfx65czjJIGKkZHe9BEZTS wj9Q== 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=TsjTBjDSxbsIZa3cyM3cuBumFZ+sSidPsK06VPX52sg=; b=h6qSBAyLeCBfgSjgZ0Lmgp9kQWk28+o7bSkWyqCKWdQGvqz0rTYE/fD3U7s6r2geP4 NIaBbVXanWZL/gTDmxwLjh+2gM26NRw+8yzLmTUtAfVfOZ6RuQPC6mq8PsB27SIGUn/5 1J4Jfs9gQGubP0h1k6M7DWZBFjGN3lT4F6WB8tEN9RVkkEtHHbsqHX/vKXS1jhRyOoZ9 /gUZW15FMk5hMtpHTHaC2kWPhuRMTxbyZf9xwJgqzZKPPccofXnk4GzwqntSS9H74sDN x0lM3fuskiYLBiuDseBXyzzn34ccs0y0Ks5LqZGJkmXUjute0pbWD9+CgAfv1sQZjuhT 5Eww== X-Gm-Message-State: AOPr4FW1SAqlxNSjHTLj3xwydDNTPdLKpzZpgjYzZzkhnGdli6fTifT3e0ByDzqH95b18a6y64SYIMF2GhNiOg== X-Received: by 10.25.158.149 with SMTP id h143mr11202638lfe.135.1462603702652; Fri, 06 May 2016 23:48:22 -0700 (PDT) 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <9D4F2568-405C-419B-9B18-7376B34143CD@cajuntechie.org> Xref: csiph.com comp.lang.python:108256 On Sat, May 7, 2016 at 4: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. Frankly, you shouldn't care. Integers and strings should always be compared for equality ("=="), not identity ("is"). Everything else is an interpreter optimization; what you'll find is that some small integers are cached deep within CPython, so the integer 7 is always the same object. The exact set that's cached varies from version to version of CPython, and other interpreters mightn't do that at all - or might not even have actual in-memory objects for *any* integers in the machine word range, using their values directly. Or anything at all. ChrisA