Problem overview
Three classes greet people. Greeter is the plain one, and PirateGreeter and FormalGreeter inherit from it. Each class has greet(name), and each child overrides the parent's version with its own:
| Class | greet("Ann") |
|---|---|
Greeter | Hello, Ann |
PirateGreeter | Ahoy, Ann! |
FormalGreeter | Good day, Ann. |
The tests read lines such as pirate Bob, make that kind of greeter and print what greet("Bob") returns.
Approach
To override a method, declare it again in the child class with the same name and the same parameters. When you call greet on an object, Python looks in the object's own class first and only then in its parents, so the child's version wins. The parent's greet is still there for plain Greeter objects.
Step by step
- Declare
greet(self, name)inGreeter. - Return
"Hello, "followed byname. Mind the comma and the space. - Declare
greetagain inPirateGreeter, with the same parameters. Same name and same parameters is what makes it an override. - Return
"Ahoy, ", thenname, then"!". - Declare
greetinFormalGreeterthe same way. - Return
"Good day, ", thenname, then".".
The solution in Python
class Greeter:
def greet(self, name):
return "Hello, " + name
class PirateGreeter(Greeter):
def greet(self, name):
return "Ahoy, " + name + "!"
class FormalGreeter(Greeter):
def greet(self, name):
return "Good day, " + name + "."
Using them for Example 1:
print(Greeter().greet("Ann"))
print(PirateGreeter().greet("Bob"))
print(FormalGreeter().greet("Cy"))
Hello, Ann
Ahoy, Bob!
Good day, Cy.
None of the classes has an __init__, so each is created with no arguments. The same name gives three different results depending on which class the object belongs to.
Python details
- Python has no keyword for overriding. Writing a method with the same name in the child is all it takes, so a typo in the name creates a new method instead. A child with
def great(self, name)leavesgreetalone, and callinggreet("Bo")still returnsHello, Bo. - Python does not check parameters when you override. A child with
def greet(self)is accepted, but calling it asgreet("Bo")fails withgreet() takes 1 positional argument but 2 were given. - A child can reuse the parent's version with
super(). This subclass returnsHello, Bo!!for"Bo":
class Excited(Greeter):
def greet(self, name):
return super().greet(name) + "!!"
The lesson doesn't need it, since each child builds its whole text itself.
Common mistakes
- Different parameters. The override must accept
namelike the parent does. - Misspelled method name. A new method is created and the override never runs.
- Wrong punctuation. The tests compare whole lines:
"Hello, Ann"has no mark at the end, the pirate one ends with!and the formal one with.. - Forgetting the parentheses after the class name.
class PirateGreeter:without(Greeter)is a separate class. Its owngreetwould still run, but it wouldn't be a kind ofGreeter, so it wouldn't inherit anything else the parent might gain later.
Testing
Example 2 gives the same name to all three kinds. The test lines are pirate Mia, formal Mia and greeter Mia:
greeters = [PirateGreeter(), FormalGreeter(), Greeter()]
for g in greeters:
print(g.greet("Mia"))
Ahoy, Mia!
Good day, Mia.
Hello, Mia
The loop doesn't ask which kind each greeter is. It calls g.greet("Mia") on each one, and each object runs the version from its own class. Python variables have no declared types, so no cast or special keyword is needed for this to work. The statement mentions a Greeter variable holding a child; in Python any variable can hold any object, and the override decides the greeting.
An example with no plain greeter, such as formal Lee, pirate Jack, formal Max, works the same way: each line gets the greeting of its own class.
