How does python know?
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: 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 Weird as well, is that in the interpreter, the introduction of punctuation appears to defeat the reuse of the object: b = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a is b True a = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' b = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a is b False b = 'la.sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a = 'la.sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a is b False a = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' b = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a is b True Tobiah -- https://mail.python.org/mailman/listinfo/python-list
Re: How does python know?
On Thu, Feb 13, 2014 at 7:17 AM, Tobiah t...@tobiah.org wrote: 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? No, it doesn't; but when you compile a module (including a simple script like that), Python checks for repeated literals. It's only good for literals, though. If you specifically need this behaviour, it's called 'interning'. You can ask Python to do this, or you can do it manually. But most of the time, you can just ignore id() and simply let two strings be equal based on their contents; the fact that constants are shared is a neat optimization, nothing more. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: How does python know?
Tobiah t...@tobiah.org Wrote in message: 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 Weird as well, is that in the interpreter, the introduction of punctuation appears to defeat the reuse of the object: b = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a is b True a = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' b = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl' a is b As others have said, interning is implementation specific, so you should never rely on it. I think the current CPython algorithm is designed to save both memory and time in the storage and look up of symbol names. In a typical program, those are the most likely to be duplicated. So if your literal is of reasonable size and doesnât invalid symbol characters (such as space, punctuation, etc) then it just might be added to the interned dictionary. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: How does python know?
In article lFQKu.455927$cz.440...@fx31.iad, Tobiah t...@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. -- https://mail.python.org/mailman/listinfo/python-list