Single Inheritance · Lecture 03 · 13 min
Overriding
A subclass overrides an inherited method by defining one with the same name, and because lookup always starts at the instance's own class, even the parent's code calls the override.
Replacing an inherited method
A class with no __str__ still works with str(), because it inherits object.__str__:
class Person:
pass
str(Person())To change that behavior, define a method with the same name in the subclass. This is overriding: the child’s definition takes the place of the inherited one for instances of the child.
class Person:
def __str__(self):
return 'Person class'
str(Person())str() falls back to your __repr__
What if we override only __repr__ and then call str()?
class Person:
def __repr__(self):
return 'Person()'
p = Person()
print(str(p), repr(p))Person has no __str__, so str(p) uses the one inherited from object. That default implementation doesn’t format anything itself: it calls repr() on the same object, and that lookup finds Person.__repr__. This is why the usual advice is “always write a __repr__; add a __str__ only if you want a different, friendlier string”.
How Python finds a method
When you write obj.method, Python searches for method in the classes of type(obj).__mro__, in order, and uses the first one it finds. The search always starts at the object’s own class, no matter where the calling code lives. A small helper makes this visible:
def defined_in(obj, name):
"""Return the first class in obj's MRO whose namespace defines `name`."""
for cls in type(obj).__mro__:
if name in cls.__dict__:
return cls.__name__
print(defined_in(p, '__repr__'))
print(defined_in(p, '__str__'))
print(defined_in(p, '__eq__'))That one rule explains everything that follows.
A parent’s method calls the child’s override
Here’s a parent that defines two methods, and a child that overrides only one of them:
class Shape:
def __init__(self, name):
self.name = name
def info(self):
return f'Shape.info called for {self.name}'
def extended_info(self):
return f'Shape.extended_info called for {self.name}', self.info()
class Polygon(Shape):
def __init__(self, name):
self.name = name # duplicated for now; super() will fix this later
def info(self):
return f'Polygon.info called for {self.name}'
p = Polygon('square')
print(p.info())p.info() finds Polygon.info first, as expected. Now call extended_info, which only Shape defines and which calls self.info() internally:
for line in p.extended_info():
print(line)The code of extended_info lives in Shape, but self is p, a Polygon. So self.info() starts its search at Polygon and finds the override. The parent’s code automatically uses the child’s version.
self.This is the core of polymorphism with inheritance: a parent class can define the overall algorithm, and children customize individual steps by overriding them.
The same mechanism with __str__ and __repr__
Let’s put the two special methods on different levels of a hierarchy:
class Person:
def __str__(self):
return 'Person.__str__ called'
class Student(Person):
def __repr__(self):
return 'Student.__repr__ called'
s = Student()
print(str(s))
print(repr(s))str(s) finds Person.__str__ (Student has none), and repr(s) finds Student.__repr__. Now let Person.__str__ delegate to __repr__:
class Person:
def __str__(self):
print('Person.__str__ called')
return self.__repr__()
class Student(Person):
def __repr__(self):
return 'Student.__repr__ called'
s = Student()
print(str(s))Same story as extended_info: the method runs in Person, but self.__repr__ is looked up starting from Student. And that’s exactly what object.__str__ does when a class only defines __repr__.
When you read inherited code and wonder “which version runs here?”, ask two questions: what is the class of the object bound to self? and what is the first class in its MRO that defines this name? You never need to consider the class where the calling code happens to be written.
Overriding __eq__ removes the inherited hash
One override comes with a side effect worth knowing. If a class defines __eq__ but not __hash__, Python sets __hash__ to None in that class, so its instances become unhashable:
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def __eq__(self, other):
return isinstance(other, Point) and (self.x, self.y) == (other.x, other.y)
print(Point(1, 2) == Point(1, 2))
print(Point.__hash__)
try:
{Point(1, 2)}
except TypeError as ex:
print('TypeError:', ex)This is deliberate. The inherited, id-based hash would make two equal points hash differently, which breaks dictionaries and sets. If equal objects should be usable as keys, override __hash__ as well, for example with return hash((self.x, self.y)).
Key takeaways
- Overriding means defining a method with the same name as an inherited one. Instances of the subclass then use the subclass version.
- Attribute lookup walks
type(obj).__mro__and stops at the first class that defines the name. It always starts at the instance’s own class. - So when a parent’s method calls
self.something(), it gets the child’s override if there is one. This lets a parent define the algorithm and children customize its steps. object.__str__callsrepr(), so a class that only defines__repr__gets a matchingstr().- Defining
__eq__without__hash__sets__hash__ = None, making instances unhashable.