Errata

Think Python

Errata for Think Python, Second Edition

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
PDF Page page 170
Where it says: "for key in db: print(key, db[key])"

You CANNOT loop using the following code "for key in db: print(key, db[key])". The correct code is "for key in db.keys(): print(key, db[key])". See https://stackoverflow.com/questions/61506573/why-am-i-getting-typeerror-gdbm-gdbm-object-is-not-iterable

Anonymous  Apr 20, 2022 
PDF Page page 108
this part: A hash is a ...work correctly.

The explanation is not clear I would like to know if I understood correctly.
if I did, use my explanation, and my way (improve on it if you like) I think it is clearer, and easier my way.
I would explain the topic this way:
A hash is a function that takes a value (of any kind) and returns a unique integer.
e.g.: if a hashing of a string 'a' is 1 nothing else can have a hash value of 1, and any string 'a' always has a hash value of 1.
Dictionaries use these integers, called hash values, to store and look up key-value pairs.
so, when we write (d is a dictionary object) d['a'] = 2 then the dictionary stores the value 2 at index 1 which is the hash of 'a'.

Mutable objects cannot be keys. I will give an example of the behavior for immutable keys, and then explain why mutable ones don't work:

ex of dictionary with immutable key:
a = 'a'
d = {a:'zzz'} # variable a is in hypothetical location: 100, with value 'a', and hypothetical hash of 1
# if we change the value of variable a to 'b'
a = 'b' # now variable a points, or references hypothetical location 200 of value 'b', but the key of the dictionary still references location 100 which has the value of 'a' , and hash 1. the key is still 'a' it did not change to 'b' so by using the key 'a' we can access the value 'zzz' which is stored at hypothetical hash of 'a' which is 1

ex of dictionary with mutable key:
a = ['a']
d= {a:'zzz'} #variable a is in hypothetical location: 100, with value ['a'], and hypothetical hash of 3
a.append('x') # now variable a still points, or references hypothetical location 100, but the value changed to ['a','x']. The problem here is that the key clearly changed to ['a','x'] which has a different hash than ['a']. so the dictionary would lool like this: d = {['a','x']:'zzz'}, but the 'zzz' value would be stored in the hash of ['a'], and we don't have any key ['a'] anymore. that is why this dictionaries are not implemented to support mutable objects as key.
mutable objects cause inconsistencies, and would lead to hard to predict, and problematic behavior.

I hope this is clear enough. did I understand the concept correctly, and can you provide a clearer explanation in the next edition?, and for me

Anonymous  Jun 28, 2024