You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: deepdiff/deephash_doc.rst
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -132,6 +132,7 @@ If you try to hash it:
132
132
TypeError: unhashable type: 'dict'
133
133
134
134
But with DeepHash:
135
+
135
136
>>> from deepdiff import DeepHash
136
137
>>> obj = {1: 2, 'a': 'b'}
137
138
>>> DeepHash(obj)
@@ -141,11 +142,13 @@ But with DeepHash:
141
142
DeepHash is calculating the hash of the obj and any other object that obj contains.
142
143
The output of DeepHash is a dictionary of object IDs to their hashes.
143
144
In order to get the hash of obj itself, you need to use the object (or the id of object) to get its hash:
145
+
144
146
>>> hashes = DeepHash(obj)
145
147
>>> hashes[obj]
146
148
34150898645750099477987229399128149852
147
149
148
150
Which you can write as:
151
+
149
152
>>> hashes = DeepHash(obj)[obj]
150
153
151
154
At first it might seem weird why DeepHash(obj)[obj] but remember that DeepHash(obj) is a dictionary of hashes of all other objects that obj contains too.
@@ -154,27 +157,32 @@ But with DeepHash:
154
157
Murmur 3 128bit hashing algorithm. If you prefer to use another hashing algorithm, you can pass it using the hasher parameter. Read more about Murmur3 here: https://en.wikipedia.org/wiki/MurmurHash
155
158
156
159
If you do a deep copy of obj, it should still give you the same hash:
160
+
157
161
>>> from copy import deepcopy
158
162
>>> obj2 = deepcopy(obj)
159
163
>>> DeepHash(obj2)[obj2]
160
164
34150898645750099477987229399128149852
161
165
162
166
Note that by default DeepHash will include string type differences. So if your strings were bytes:
167
+
163
168
>>> obj3 = {1: 2, b'a': b'b'}
164
169
>>> DeepHash(obj3)[obj3]
165
170
64067525765846024488103933101621212760
166
171
167
172
But if you want the same hash if string types are different, set ignore_string_type_changes to True:
You can pass a list of tuples or list of lists if you have various type groups. When t1 and t2 both fall under one of these type groups, the type change will be ignored. DeepDiff already comes with 2 groups: DeepDiff.strings and DeepDiff.numbers . If you want to pass both:
So both lists produced the same hash thanks to the low significant digits for 100000 vs 100021 and also the custom_number_to_string that converted all numbers below 100 to be 100!
0 commit comments