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


Groups > comp.lang.python > #66101

Re: How does python know?

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 <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.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 <davea@davea.name>
Subject Re: How does python know?
Date Wed, 12 Feb 2014 16:59:28 -0500 (EST)
Organization news.gmane.org
References <lFQKu.455927$cZ.440055@fx31.iad> <pOQKu.37378$vk5.11310@fx03.iad>
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 <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>
Newsgroups comp.lang.python
Message-ID <mailman.6797.1392242168.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


 Tobiah <toby@tobiah.org> 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

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


Thread

How does python know? Tobiah <toby@tobiah.org> - 2014-02-12 12:17 -0800
  Re: How does python know? Tobiah <toby@tobiah.org> - 2014-02-12 12:27 -0800
    Re: How does python know? Dave Angel <davea@davea.name> - 2014-02-12 16:59 -0500
  Re: How does python know? Chris Angelico <rosuav@gmail.com> - 2014-02-13 07:33 +1100
  Re: How does python know? Gary Herron <gary.herron@islandtraining.com> - 2014-02-12 13:02 -0800
  Re: How does python know? Roy Smith <roy@panix.com> - 2014-02-12 20:54 -0500

csiph-web