June 2019
Beginner to intermediate
770 pages
19h 24m
English
An alternative to wrapping is to extend a built-in class. By doing this, we have the advantage of not having to reimplement the pop() method; we can simply inherit it.
The pop() method has an advantage in that it creates a class without writing too much code. In this example, extending the list class has the disadvantage that this provides many more functions than we truly need.
The following is a definition of Deck2 that extends the built-in list object:
class Deck2(list): def __init__(self) -> None: super().__init__( card(r + 1, s) for r in range(13) for s in iter(Suit)) random.shuffle(self)
In this case, we've initialized the list with Card instances. super().__init__() reaches up to the superclass initialization ...
Read now
Unlock full access