Python intern for non-strings

2019-01-09 15:22发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

Only strings are supported because interning relies on a pointer-based object identity test. Hashes of other types of classes could be compared, but the objects themselves will never match an identity test. This is true because even though they may be identical, they are not the same objects.

Reference