I am not at all unfamiliar with the concept of:
TypeError: unhashable type: 'OrderedDict'
But I can not understand how the following line of codes can produce such a stack-trace.
89: @staticmethod
90: def diff(var1, var2, path=[], level=0, curpath=[]):
...
101: elif isinstance(var1, list) and isinstance(var2, list):
102: l1s = set(var1)
103: l2s = set(var2)
104: retlist = []
File "myFile.py", line 102, in diff
l1s = set(var1)
TypeError: unhashable type: 'OrderedDict'
How can line 102
, in the above code throw such an exception?
This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.
TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.
Adding a NumPy Array to a Set We are attempting to add a ndarray to a set in this case. Therefore, the code above should return an error: We can solve this by adding each array element instead of the array object into the set. This should add all the elements of the array to the set.
The Python "TypeError: unhashable type: 'set'" occurs when we use a set as a key in a dictionary or an element in another set . To solve the error, use a frozenset instead, because set objects are mutable and unhashable. Here are 2 examples of how the error occurs.
Some data structures (most notably dict
s and set
s) require objects they contain (keys in the case of dictionaries, items in the case of sets) to implement the __hash__()
magic method, so that calling hash(obj)
returns a value.
This is required to optimize the structure, and together with immutability to help guarantee uniqueness of contained objects.
In your case, var1
contains some object that is not hashable (it does not implement hash()
). This object is an OrderedDict
, which is a mutable object and is not hashable by design.
As an example of an other object type which is mutable and not hashable by design, consider list
and this example:
>>> L = [1, 2, 3]
>>> set([L])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> hash(L)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
If you're using set()
to ensure uniqueness then you must go some other way, though it's not clear from your question.
dict(including OrderedDict) in python are mutable containers.
If a dict was hashed, its hash value would be changed as long as you changed the dict's contents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With