Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Tuple[Hashable] mean in Python?

I came across the following piece of code:

def func(self, v: Tuple[Hashable]):
...

I know v: Tuple would mean variable v must be of type Tuple, but what does Tuple[Hashable] mean? Isn't a tuple in Python always hashable?

like image 938
traveh Avatar asked Oct 17 '25 17:10

traveh


1 Answers

A tuple is only hashable if the values in the tuple are hashable themselves.

>>> hash((1,2))
-3550055125485641917
>>> hash(([1],2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
like image 92
chepner Avatar answered Oct 20 '25 11:10

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!