list - Why does the Python Docs state that "all immutable built-in objects are hashable"? -
on glossary-page under section "hashable" of official python documentation visitors can read that
an object hashable if has hash value never changes during lifetime ...
all of python’s immutable built-in objects hashable, while no mutable containers (such lists or dictionaries) not...
this means passing object of the
- int,- float,- long,- complex,
- str,
- bytes,
- tupleor
- frozenset
class build-in hash() method must return supposed hash-value.
the problem tuples can contain unhashable objects (such lists) , therefor some tuples not hashable:
- create (valid) tuple consisting of hashable ( - ints ,- string) , unhashable (- list) data types.- >>> tuple([1, 2, [3, "4"]]) (1, 2, [3, '4'])
- hashing tuple fails ... - >>> hash((1, 2, [3, '4'])) traceback (most recent call last): hash((1, 2, [3, '4'])) typeerror: unhashable type: 'list'
- ... although object hash immutable built-in type - >>> type((1, 2, [3, '4'])) <class 'tuple'>
so, why python docs state "all immutable built-in objects hashable", although tuple type can contain unhashable types?
i don't know python's hashing, me looks you're nitpicking text.
all of python’s immutable built-in objects hashable
that's stated , that's true: can hash tuple such (3, 3, 2) fine, tuples hashable.
however, if put unhashable list tuple, can no longer hashed because contains unhashable object. doesn't mean tuples unhashable, lists are, , tuple contains list can no longer hashed.
placing non-compostable garbage compostable garbage bag doesn't make bag non-compostable.
Comments
Post a Comment