Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

hash()

Computes a hash code used by sets and dictionaries for hashable objects.

Version: Python 3.14; repository image, production runtime not attested

Syntax

hash(object, /)

Computes a hash code used by sets and dictionaries for hashable objects.

Equal numeric values must have equal hashes. Mutable lists cannot be hashed.

Returns the integer hash used by hashed collections. Equal hashable objects must have equal hashes, so 1 and 1.0 give the same hash. The converse is false: collisions occur. Ordinary lists are unhashable and raise TypeError. Hashes such as those of strings need not be stable across processes.

assert hash(1) == hash(1.0) == hash(True)
class Clash:
    def __hash__(self): return 1
a, b = Clash(), Clash()
assert a != b and hash(a) == hash(b)

Examples

print(hash(1) == hash(1.0))
try:
    hash([1])
except TypeError as error:
    print(type(error).__name__)

Result

True
TypeError

Returns the integer hash used by hashed collections. Equal hashable objects must have equal hashes, so 1 and 1.0 give the same hash. The converse is false: collisions occur. Ordinary lists are unhashable and raise TypeError. Hashes such as those of strings need not be stable across processes.

Sources