all Python 3Built-in functionsterm
hash
introspection
Returns the integer hash of an object. The object must be hashable: its hash never changes during its lifetime, and equal objects have equal hashes. Used by dictionaries and sets.
Syntax
hash(object)
For str and bytes the hash is randomised on every interpreter start (PYTHONHASHSEED) — it is a collision-attack defence, not a checksum, so never persist it or compare it across processes; use hashlib for a stable fingerprint. The contract also says equal objects must hash equally: hash(1) == hash(1.0) == hash(True), and your own class turns unhashable the moment you define __eq__ without __hash__.
Examples
print(hash(42)) # → 42
print(hash('hello')) # → ? хеш строки солится PYTHONHASHSEED и меняется от запуска
print(hash((1,2,3))) # → ? хеш кортежа считается из хешей элементов
try:
hash([1,2,3]) # список нехешируем — будет TypeError
except TypeError as e: print(e) # → unhashable type: 'list'
print(hash(42) == hash(42.0)) # → True (равные объекты)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy