What does hash do in python? -


i saw example of code hash function applied tuple. result returns negative integer. wonder function does. google not help. found page explains how hash calculated not explain why need function.

a hash fixed sized integer identifies particular value. each value need have it's own hash, same value same hash if it's not same object.

>>> hash("look @ me!") 4343814758193556824 >>> f = "look @ me!" >>> hash(f) 4343814758193556824 

hash values need created in such way resulting values evenly distributed reduce number of hash collisions get. hash collisions when 2 different values have same hash. therefore, relatively small changes result in different hashes.

>>> hash("look @ me!!") 6941904779894686356 

these numbers useful, enable quick look-up of values in large collection of values. examples of use in python's set , dict. in list, if want check if value in list, if x in values:, python needs go through whole list , compare x each value in list values. can take long time long list. in set, python keeps track of each hash, , when type if x in values:, python hash-value x, in internal structure , compare x values have same hash x.

the same methodology used dictionary lookup. makes lookup in set , dict fast, while lookup in list slow. means can have non-hashable objects in list, not in set or keys in dict. typical example of non-hashable objects object mutable, ie, can change it. if have mutable object should not hashable, it's hash change on it's life-time, cause lot of confusion, object end under wrong hash value in dictionary.

note hash of value needs same 1 run of python. in python 3.3 in fact change every new run of python:

$ /opt/python33/bin/python3 python 3.3.2 (default, jun 17 2013, 17:49:21)  [gcc 4.6.3] on linux type "help", "copyright", "credits" or "license" more information. >>> hash("foo") 1849024199686380661 >>>  $ /opt/python33/bin/python3 python 3.3.2 (default, jun 17 2013, 17:49:21)  [gcc 4.6.3] on linux type "help", "copyright", "credits" or "license" more information. >>> hash("foo") -7416743951976404299 

this make harder guess hash value string have, important security feature web applications etc.

hash values should therefore not stored permanently. if need use hash values in permanent way can take @ more "serious" types of hashes, cryptographic hash functions, can used making verifiable checksums of files etc.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -