osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How does python know?


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