How does python know?

Gary Herron gary.herron at islandtraining.com
Wed Feb 12 22:02:03 CET 2014


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

Yes.

Kind of:  It's a hash calculation not a direct comparison, and it's 
applied to strings  of limited length.  Details are implementation (and 
perhaps version) specific.  The process is called "string interning".  
Google and wikipedia have lots to say about it.

Gary Herron



More information about the Python-list mailing list