Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; 'cpython': 0.05; 'string': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'strings.': 0.09; 'subject:How': 0.10; 'python': 0.11; 'wrote': 0.14; 'dictionary.': 0.16; 'interpreter,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'specific,': 0.16; 'subject:python': 0.16; 'weird': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; '>>>': 0.22; 'appears': 0.22; 'memory': 0.22; 'print': 0.22; '(such': 0.24; 'typical': 0.24; "i've": 0.25; 'compare': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'characters': 0.30; 'said,': 0.30; 'program,': 0.31; 'names.': 0.31; "skip:' 40": 0.31; 'subject:?': 0.36; 'should': 0.36; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'most': 0.60; 'new': 0.61; 'save': 0.62; 'determine': 0.67; 'introduction': 0.68; 'invalid': 0.68; 'object:': 0.84; 'subject:know': 0.84; 'defeat': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: How does python know? Date: Wed, 12 Feb 2014 16:59:28 -0500 (EST) Organization: news.gmane.org References: X-Gmane-NNTP-Posting-Host: dpc6744192124.direcpc.com X-Newsreader: PiaoHong Usenet NewsReaders 1.36 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392242168 news.xs4all.nl 2923 [2001:888:2000:d::a6]:46912 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66101 Tobiah Wrote in message: > On 02/12/2014 12:17 PM, Tobiah wrote: >> I do this: >> >> a = 'lasdfjlasdjflaksdjfl;akjsdf;kljasdl;kfjasl' >> b = 'lasdfjlasdjflaksdjfl;akjsdf;kljasdl;kfjasl' >> >> print >> print id(a) >> print id(b) >> >> >> And get this: >> >> True >> 140329184721376 >> 140329184721376 >> >> >> 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? >> >> Thanks, >> >> Tobiah > > Weird as well, is that in the interpreter, > the introduction of punctuation appears to > defeat the reuse of the object: > > >>> b = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' > >>> a = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' > >>> a is b > True > >>> a = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' > >>> b = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' > >>> a is b > > As others have said, interning is implementation specific, so you should never rely on it. I think the current CPython algorithm is designed to save both memory and time in the storage and look up of symbol names. In a typical program, those are the most likely to be duplicated. So if your literal is of reasonable size and doesn’t invalid symbol characters (such as space, punctuation, etc) then it just might be added to the interned dictionary. -- DaveA