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,
  • tuple or
  • 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:

  1. create (valid) tuple consisting of hashable (ints , string) , unhashable (list) data types.

    >>> tuple([1, 2, [3, "4"]])       (1, 2, [3, '4']) 
  2. hashing tuple fails ...

    >>> hash((1, 2, [3, '4']))      traceback (most recent call last):         hash((1, 2, [3, '4']))     typeerror: unhashable type: 'list' 
  3. ... 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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -