Why are some mutable MNE objects hashable?
See original GitHub issueSeveral (mutable) MNE objects (e.g. Raw) are hashable, which violates one of the key assumptions that Python makes about hashable objects, namely that the hash value never changes during its lifetime. This can lead to bugs wherever hashable objects are expected not to change (so everywhere, such as using them as keys in dicts). @larsoner mentioned that this was done to support caching in joblib, but it would probably be a good idea to discuss other (Pythonic) solutions. Thoughts?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Why mutable built-in objects cannot be hashable in Python ...
In Python, mutable objects can be hashable, but it is generally not a good idea, because generally speaking, the equality is defined in...
Read more >Hashable Objects Must Be Immutable
The only exception when you can have a mutable, hashable class is when the hash is based on the identity and not the...
Read more >Immutable vs. Hashable
Immutable objects are a type of object that cannot be modified after they were created. Hashable objects, on the other hand, are a...
Read more >Why can't mutable built-in objects be hashable in Python? ...
Mutable objects by definition can be changed. That means, you could change it and the hash might no lomger be appropriate for the...
Read more >Iterable, Ordered, Mutable, and Hashable Python Objects ...
Iterable. An iterable object in Python is an object that can be looped over for extracting its items one by one or applying...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Given that usually (or at least, often) user-defined classes are made to be mutable, it seems like a contradiction for Python to make user-defined classes by default be hashable, but say that hashable objects should not change.
So there is at least some inconsistency there, and at least our
__hash__functions are designed to make it so that if you do change something about the object (e.g., info or data), the__hash__will also change (where it wouldn’t with the defaultid-based implementation).I’ll close assuming we can’t come up with a different way to use this. But in case you want to investigate @cbrnr here is a snippet (constructed quickly just from looking at the joblib docs), you could imagine the
time.sleepcould be any time-consuming process:Produces: