Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'string': 0.09; 'repeated': 0.09; 'strings.': 0.09; 'subject:How': 0.10; 'cc:addr:python- list': 0.11; 'python': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literals,': 0.16; 'literals.': 0.16; 'subject:python': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'module': 0.19; 'thu,': 0.19; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; "i've": 0.25; 'script': 0.25; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '13,': 0.31; 'though.': 0.31; '(including': 0.33; 'equal': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'two': 0.37; 'checks': 0.38; 'fact': 0.38; 'does': 0.39; 'called': 0.40; 'most': 0.60; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; 'determine': 0.67; 'subject:know': 0.84; 'that),': 0.91; 'to:none': 0.92 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 :content-type; bh=pzBRb0pu8lb3sr0cwdGdrlHZeBgJmaNN1ysRUBt/nIc=; b=khOwiu9ykKS6WTPHyqtSE1id0kzUgl6WcfH8NF5+y5lNMANXPNsC2cOl8FFSypsC4c rz9nsSoZ59mxdtEktotjh9IG90mUvHZABC4osKiGRtCBnwX3GBydrUGdNn4C675YRTxw 75nW4Bta8+YW7iF08eD9sUHgolcgZR7QFmbO/H3x/z8hq34/mjax4T7SyHUDSRKcb+WI dNU6bfs9LhHkp2S/xCZjL2HJTUkGraSvtaNOhiJwwJeIjt3MBud86qLV/WNL3jYYqZ12 5XUCVhTAlMm9E860LXiW3gFqcalxlQBKPERwjlBTMj0UTKr49eQWrjbIh/R3aBH+PEIP 8E9w== MIME-Version: 1.0 X-Received: by 10.68.98.3 with SMTP id ee3mr53935808pbb.31.1392237221081; Wed, 12 Feb 2014 12:33:41 -0800 (PST) In-Reply-To: References: Date: Thu, 13 Feb 2014 07:33:40 +1100 Subject: Re: How does python know? From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 17 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392237224 news.xs4all.nl 2924 [2001:888:2000:d::a6]:55944 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66079 On Thu, Feb 13, 2014 at 7:17 AM, Tobiah wrote: > This works for longer strings. Does python > compare a new string to every other string > I've made in order to determine whether it > needs to create a new object? No, it doesn't; but when you compile a module (including a simple script like that), Python checks for repeated literals. It's only good for literals, though. If you specifically need this behaviour, it's called 'interning'. You can ask Python to do this, or you can do it manually. But most of the time, you can just ignore id() and simply let two strings be equal based on their contents; the fact that constants are shared is a neat optimization, nothing more. ChrisA