0

Some objects, numpy const arrays for example, are perfectly hashable but happened not to be (due to __eq__ being repurposed as an arithmetic operator, in the numpy case). Some objects, numpy arrays again for example, have multiple defintions of equility (id-equility and value-equility). For those object, a dict with user provided key function (as in sorted) would be very useful.

My question is: Is there a dict with user provided key function, either builtin or implemented in few lines, in Python?

Huazuo Gao
  • 1,603
  • 14
  • 20
  • Do you just want to modify the default behaviour of the `keys()` method? Or do you men something else? – match Feb 27 '20 at 16:24
  • I would like a `key:Callable=None` argument in the dict constructor, which is then used for comparing keys, just like what `list.sort` or `sorted` does. – Huazuo Gao Feb 27 '20 at 16:27
  • 2
    Maybe have a look at `collections.UserDict` which lets you construct classes tht behave like `dict`s? – match Feb 27 '20 at 16:29
  • I found a closely related question [How to “perfectly” override a dict?](https://stackoverflow.com/q/3387691/4442250) – Huazuo Gao Feb 27 '20 at 16:44

0 Answers0