2015-12-21 03:05:20 +00:00
|
|
|
.. _caveat-hashable-values:
|
|
|
|
|
2015-03-22 18:21:15 +00:00
|
|
|
Values Must Be Hashable
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
Because you must be able to look up keys by value as well as values by key,
|
|
|
|
values must also be hashable.
|
|
|
|
|
|
|
|
Attempting to insert an unhashable value will result in an error::
|
|
|
|
|
2015-12-21 03:05:20 +00:00
|
|
|
>>> from bidict import bidict
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> anagrams_by_alphagram = bidict(opt=['opt', 'pot', 'top'])
|
|
|
|
Traceback (most recent call last):
|
2015-12-21 03:05:20 +00:00
|
|
|
...
|
|
|
|
TypeError...
|
|
|
|
|
|
|
|
In this example, using a tuple instead of a list does the trick,
|
|
|
|
and confers additional benefits of immutability::
|
|
|
|
|
2015-03-22 18:21:15 +00:00
|
|
|
>>> bidict(opt=('opt', 'pot', 'top'))
|
|
|
|
bidict({'opt': ('opt', 'pot', 'top')})
|