Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if hashable
#2
You could use collections.abc.Hashable

>>> from collections.abc import Hashable
>>> isinstance('spam', Hashable)
True
>>> isinstance([], Hashable)
False
Well, thinking again, it doesn't seem to work well for what you want to do
>>> from collections.abc import Hashable
>>> isinstance((1, []), Hashable)
True
>>> {(1, []): 3}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Then a reason to prefer hash() over the dict is that it doesn't attempt to build a dictionary instance.
Reply


Messages In This Thread
if hashable - by Skaperen - Sep-24-2023, 01:14 AM
RE: if hashable - by Gribouillis - Sep-24-2023, 09:02 AM
RE: if hashable - by Skaperen - Sep-24-2023, 05:26 PM
RE: if hashable - by buran - Sep-25-2023, 07:48 AM
RE: if hashable - by Skaperen - Sep-25-2023, 11:24 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020