A brief note on Classes and Inheritance: objects and classes, __init__ and self, state and its rules, class and object attributes, inheritance with super(), overriding, special methods, and is a or has a. With a worked example, practice, a program and questions to check yourself.
A brief note on Classes and Inheritance. Read it before a test. Try each question at the end before you open its answer.
type(x) gives the class of x.word.upper(). An attribute is read without them, z.real.class Name: with methods inside. __init__(self, ...) sets up each new object, and Name(arguments) makes one.self is the object that the method was called on. It is always the first parameter.self.level = 0 makes data that belongs to this one object. Each object has its own.b = a gives the same object a second name. It is not a copy.items=None, and make a new list inside __init__.class Child(Parent): builds one class on another. The child gets everything and adds its own.__init__ calls super().__init__(...), so the parent sets up its part.super().method() runs the parent's version, to extend it.isinstance(x, Parent) is true for the parent and for every class built on it.Python calls these for you. They have two underscores on each side.
| Method | Makes this work |
|---|---|
__str__ | print(x) and str(x) |
__repr__ | a list of objects, for programmers |
__eq__ | == |
__lt__ | <, sorted, min and max |
__add__ | +, returning a new object |
__len__ | len(x) |
__contains__ | in |
__str__ and __repr__ return text. They do not print it.A water tank keeps its level in litres, and refuses an action that breaks its rules.
Follow the state after each action, including the refused one.
| Action | level | Why |
|---|---|---|
Tank(500) | 0 | __init__ sets it |
fill(320) | 320 | 0 + 320 fits |
fill(250) | 320 | 570 is over 500: refused |
draw(120) | 200 | 120 is not more than 320 |
fill(250) | 450 | 200 + 250 fits |
raise runs before self.level += litres, so the level stays at 320 litres.Tank(800) would start at 0 litres, whatever t holds.The program shows Refused: the tank would overflow and then 450 litres.
A cinema has three kinds of ticket. Two child classes override price, and __lt__ lets sorted put the tickets in order of price.
self in the def line. def price(): and then t.price() stops with TypeError: T.price() takes 0 positional arguments but 1 was given.self. in __init__. capacity = capacity makes a local name that vanishes. Later t.capacity stops with AttributeError: 'Tank' object has no attribute 'capacity'.super().__init__(...) in a child with its own __init__. The parent's attributes are never made, and reading one gives an AttributeError.__str__ that prints. print(x) then stops with TypeError: __str__ returned non-string (type NoneType). Return the text.prise adds a new method. The parent's price still runs, and nobody notices.Lamp sets self.on = False in __init__, and its method switch sets self.on = not self.on. A program runs a = Lamp(), b = a and b.switch(). What is a.on now?class Teacher(Timetable), or a Timetable that keeps a list of Teacher objects? Why?Parcel stores self.kg and has __lt__ that compares self.kg < other.kg. For parcels of 3 kg, 7 kg and 5 kg, what do max(parcels) and sorted(parcels) give?Bird has move(), which returns "flies". Its child Penguin defines def mvoe(self): return "swims". What does Penguin().move() give?__add__ method of a class Distance return a new Distance, and not change self?Log in to join the discussion.
Loading…