Why is Python's intern
built-in only for strings? It should be possible to extend intern
to classes that are hashable and comparable, right?
Just like most other modern programming languages, Python also does String Interning to gain a performance boost. In Python, we can find if two objects are referring to the same in-memory object using the is operator.
By using intern you ensure that you never create two string objects that have the same value - when you request the creation of a second string object with the same value as an existing string object, you receive a reference to the pre-existing string object. This way, you are saving memory.
In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned.
The C# Intern() method is used to retrieve reference to the specified String. It goes to intern pool (memory area) to search for a string equal to the specified String. If such a string exists, its reference in the intern pool is returned.
The purpose of interning things is to be able to compare them by comparing their memory address; you ensure that you never create two objects with the same value (when the program requests the creation of a second object with the same value as an existing object, it instead receives a reference to the pre-existing object). This requires that the things you're interning be immutable; if the value of an interned object could change, comparing them by address isn't going to work.
In Python, it's not possible to enforce the immutability of user-defined class instances, so it wouldn't be safe to intern them. I suspect that's the main theoretical reason intern doesn't cover class instances.
Other built in immutable types are either comparable in a single machine-level operation already (int, float, etc), or immutable containers that can contain mutable values (tuple, frozenset). There's no need to intern the former, and the latter can't be safely interned either.
There is no technical reason that, say, a tuple could not be interned, though I would imagine that in the real world this is of little value compared to string literals, and it would be of even less real-world value with user-defined types. Making it work is probably not considered worth the effort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With