I know that, if a class implements __hash__, it’s not mandatory that the instances are immutable. A mutable object can calculate the hash from its immutable properties. The classical example is an iterator.
If so, __repr__() can return another result if the internal state changes, or it’s not considered a good practice to do so?
I’m asking this because I’m implementing with the Python C API a builtin frozendict. I already implemented it in pure Py, but obviously is more slow than dict for some operations.
Since it’s immutable, I thought I can cache its repr when it’s hashable, that it means that all its values are hashable. But what if one of its values changes?
Alternatively, there’s not a generic way to check if a mutable object changed its version? For example, dict has a ma_version_tag integer property that increments itself when the dictionary changes.
Why do you want to cache __repr__? repr’s job is to help humans understand and debug, so I’d think reliability is extremely important and speed hardly matters at all…