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 id(a)
> print id(b)
> And get this:
> 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?
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.