osdir.com


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

How does python know?


In article <lFQKu.455927$cZ.440055 at fx31.iad>, Tobiah <toby at tobiah.org> 
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?

Yes[*].  It's called interning.  See 
https://en.wikipedia.org/wiki/Intern_(computer_science).

[*] Well, nothing requires Python to do that.  Some implementations do.  
Some don't.  Some do it for certain types of strings.  Your mileage may 
vary.