How does python know?
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 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.
More information about the Python-list