question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why are some mutable MNE objects hashable?

See original GitHub issue

Several (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:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
larsonercommented, Mar 3, 2020

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 default id-based implementation).

0reactions
larsonercommented, Apr 27, 2020

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.sleep could be any time-consuming process:

import time
import numpy as np
import joblib
import mne

data = np.random.RandomState(0).randn(1, 1000)
info = mne.create_info(1, 1000., 'eeg')
raw = mne.io.RawArray(data, info)
mem = joblib.Memory('.')


@mem.cache
def slow(raw):
    time.sleep(1.)


for _ in range(2):
    t0 = time.time()
    slow(raw)
    print(time.time() - t0)

Produces:

$ python -u cache.py 
________________________________________________________________________________
[Memory] Calling __main__--home-larsoner-Desktop-cache.slow...
slow(<RawArray | 1 x 1000 (1.0 s), ~15 kB, data loaded>)
_____________________________________________________________slow - 1.0s, 0.0min
1.008265733718872
0.001148223876953125
$ python -u cache.py 
0.003644227981567383
0.0012600421905517578
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found