|
Quoting from unit 5 lecture 10: Then I'm asking: what is this way? I don't understand how Python can do comparison between 2 immutable objects in such a way that it doesn't depend on the object size. And why the immutability is related in this? To me, if 2 objects are distinct from each other, then you have to compare each part of the object (that means characters for strings) to tell if these objects are equal. |
|
olol, it was written on wikipedia too: The practice of always using references in place of copies of equal objects is known as interning. If interning is used, two objects are considered equal if and only if their references, typically represented as integers, are equal. Some languages do this automatically: for example, Python automatically interns short strings. If the algorithm which implements interning is guaranteed to do so in every case that it is possible, then comparing objects for equality is reduced to comparing their pointers, a substantial gain in speed in most applications. (Even if the algorithm is not guaranteed to be comprehensive, there still exists the possibility of a fast path case improvement when the objects are equal and use the same reference.) Interning is generally only useful for immutable objects. I should have read it before asking... I should have read the python spec before answering so we're even :) 1
So it looks like creating a short string involves the Python environment checking if the string already exists, if so, it reuses the same string (e.g., its object ID). |
|
Reading this should help. In particular, although string literals are automatically interned, everything else that comes from outside (if you, say, read a string from a file or from the network), won't be automatically interned unless you really ask for it.
link
This answer is marked "community wiki".
|