A class does not have to start from nothing. A Dog is a kind of Animal, and a savings account is a kind of bank account. In this course you build one class on another with inheritance, replace a method in the child class, and let objects of different classes answer the same call in their own way, which is called polymorphism. You teach your own objects to work with print, equality, sorting, adding and len by writing special methods, and you learn when to build on a class and when to put one object inside another. A deck of playing cards is the project at the end. The practice runs your classes on real cases and tells you which ones fail.
5 lessons, about 2 h 25 min5 simulations7 problems10 practice questionsPython in every lesson
Inheritance builds one class on another. A child class, written with the parent in brackets, gets all the attributes and methods of the parent and can add its own, so the shared code is written once. isinstance asks whether an object is of a class or of a class built on it, and issubclass asks about two classes. A child that needs more data writes its own __init__ and calls the parent set-up with super, and if it forgets, the attributes of the parent are missing. Use inheritance only when the child really is a kind of the parent.
A child class can override a method of the parent by writing a method with the same name, and Python uses the child's version for the child's objects. Objects of different classes can answer the same call in their own way, which is called polymorphism, so code can treat a list of mixed objects alike. A child can also extend a method by calling the parent's version with super and adding to it. A parent can promise a method that every child must write, by raising NotImplementedError, and a method of the parent can call that promised method and so work for every child.
Special methods have names with two underscores on each side, and Python calls them for you when an object meets print, str, repr, equality, ordering, plus, minus, times, abs, len, in, indexing or truth. __str__ gives the text that print shows and __repr__ the text for programmers, which a list of objects shows. __eq__ says when two objects are equal, and without it two objects with the same values are different. __lt__ lets sorted, min and max put objects in order. __add__ and its relatives make operators work, __len__ and __contains__ make len and in work, and __getitem__ lets a loop go through the object. These make your objects feel like the built-in values.
Two ways to build a class from other classes are inheritance, which says is a, and composition, which says has a, where an object keeps other objects in its attributes. A Dog is an Animal, but a Car is not an Engine, it has an Engine. Composition is more flexible, because the parts can change, and it avoids long family trees. A Garage that has a list of Cars, and a Car that has an Engine, are composition. A rule of thumb is to prefer composition, and to use inheritance only for a clear is a. When a design feels hard, draw the objects and the arrows and read each arrow as is a or has a.
In this project we build a deck of playing cards. A Card knows its rank and suit, and has special methods so that two cards can be compared for equality, put in order by rank and then by suit, and shown as text. A Deck has fifty-two Cards, which is composition, and can say how many cards are left, shuffle, and deal a number of cards, refusing when there are not enough. We plan the expected results by hand, build the classes, test the order of cards and the refused deal, and extend the project with a hand class and a simple game.
Log in to join the discussion.
Loading…