2

I need to use sequence of items as dict key and to feat List[...] type. If I use tuple, than it does not feat to a List[...] type and I cannot use Tuple[...] type, because tuple length is not known.

Is there any class (maybe from a 3rd party package) to be like hashable frozen list?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Yaroslav Kishchenko
  • 475
  • 1
  • 4
  • 15
  • Does this answer your question? [Hashing arrays in Python](https://stackoverflow.com/questions/7027199/hashing-arrays-in-python) – Yannick Funk Oct 02 '20 at 21:08
  • @YannickFunk, so I need to subclass `list` and provide `__hash__` method manually? Is there any package I could install for that, which could also freeze the list? Unfortunately `frozenlist` package does not provide hashing out of the box( – Yaroslav Kishchenko Oct 02 '20 at 21:11

2 Answers2

1

The immutable equivalent to List[T] is Tuple[T, ...].

From the Python documentation:

To specify a variable-length tuple of homogeneous type, use literal ellipsis, e.g. Tuple[int, ...]. A plain Tuple is equivalent to Tuple[Any, ...], and in turn to tuple.

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
0

It appeared that frozenlist package have needed functionality

    def __hash__(self):
        if self._frozen:
            return hash(tuple(self))
        else:
            raise RuntimeError("Cannot hash unfrozen list.")

But this code is not included in any release (1.0.0 and 1.0.0a0 for that moment). Check if the situation will be improved. Here is the issue I created.

Yaroslav Kishchenko
  • 475
  • 1
  • 4
  • 15