Substitutability asks whether code written for the base can use a subclass without losing the base’s promised behavior. The phrase “is a” helps generate a candidate relationship, but the actual interface and its rules decide whether it works.
Suppose a rectangular frame promises independent width and height changes. A square has equal sides, so an implementation that changes both dimensions whenever width changes would violate that promise. The mathematical relationship alone does not settle the software design.
class Frame:
def __init__(self, width, height):
self.width = width
self.height = height
def set_width(self, width):
self.width = width
def widen(frame):
frame.set_width(8)
return (frame.width, frame.height)
frame = Frame(3, 4)
print(widen(frame))
Output:
(8, 4)
The caller expects the height to stay at four. A hypothetical square subclass that changes height to eight would make this call return (8, 8) instead. This is a design conflict, not a Python syntax error. A different, read-only shape interface that promises only an area could allow both types to satisfy the same caller; a separate square type is another choice.
Check accepted inputs, return meaning, allowed state changes and failures. An override should not demand extra conditions for a call the base accepts or remove a result the base promises. Context matters: a child may add capabilities for callers that explicitly know about it, while still honoring base operations.