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


Groups > comp.lang.python > #66101

Re: How does python know?

From Dave Angel <davea@davea.name>
Subject Re: How does python know?
Date 2014-02-12 16:59 -0500
Organization news.gmane.org
References <lFQKu.455927$cZ.440055@fx31.iad> <pOQKu.37378$vk5.11310@fx03.iad>
Newsgroups comp.lang.python
Message-ID <mailman.6797.1392242168.18130.python-list@python.org> (permalink)

Show all headers | 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