Chapter 9, dictionary order as of Python 3.7 no longer unpredictable
I noticed an inaccuracy due to a change in python 3.7 that, to my understanding, made it a language feature that dictionaries are ordered by insertion order.
The order of the key-value pairs is not the same. In fact, if you type the same example on your computer, you might get a different result. In general, the order of items in a dictionary is unpredictable.
This means one of the questions in the chapter 9 quiz on Coursera is now ambiguous and based on which version of Python 3 you're using.
Thanks - I will keep this open. Someday we will redo everything (book, slides, videos) for use with a later version of Python and catch this. I will just hold the issue open as a reminder.
Tom - they day has finally come. I just finished a major redo of Chapter 9 and all the associated lectures. Feel free to take a look, review and comment - https://do1.dr-chuck.com/pythonlearn/EN_us/pythonlearn.pdf (sorry this too way too long)
Wonderful, congratulations on completing this work! I did take a look through chapter 9 (and the rest as a refresher).
A mistake I remember first making when starting with Python, and was reminded of by the start of chapter 9, was the distinction between:
type({})
type({'a'})
I expected {} to create an empty set not an empty dict as I was thinking about it like []. It might be worth mentioning set's use of curly brackets to help prevent future confusion when learners come across sets.
I hate to say this on the heels of the completion of such a task, but have you considered adding a chapter between 14 and 15 mentioning dataclasses introduced in 3.7? I make quite heavy use of them now especially as I can define derived @property and set defaults.
Examples re-imprementing the classes in chapter 14 might be quite instructive.
from dataclasses import dataclass
@dataclass
class PartyAnimal:
name: str
x: int = 0
def __post_init__(self):
print(self.name, 'constructed')
def party(self):
self.x += 1
print(self.name, "party count", self.x)
def __del__(self):
print(self.name, 'destructed', self.x)
class CricketFan(PartyAnimal):
points: int = 0
def six(self):
self.points += 6
self.party()
print(self.name, "points", self.points)
NamedTuples also underwent some changes but to me seem to have stabilized and there may be benefits in contrasting the two and mentioning any differences between typing.NamedTuple and collections.namedtuple.