Liskov substitution principle (was: list(), tuple() should not place at "Built-in functions" in documentation)

Ben Finney ben+python at
Sat Jul 16 02:04:36 CEST 2011

Chris Angelico <rosuav at> writes:

> The analogy with reality breaks down a bit here. I've seen plenty of
> students with no idea of what it means to study. But Python can handle
> that too - just 'del' the method in the subclass.

No, please don't. That would break the Liskov substitution principle

By inheriting from a type that provides a method, you're promising that
you will implement at least as much behaviour as the parent. If that
includes a ‘study’ method, then your subclass must also implement (or
inherit) that method.

Code can then be written to expect that, so long as the object inherits
from Student, it will at least have the same minimum level of behaviour
that a Student has.

If you inherit from Student, but delete the ‘study’ method, you would
break any code which assumes any Student has a ‘study’ method –
something that was explicitly promised in the API of Student.

Since you are advocating calling people students when they don't study,
it sounds instead like you want a different set of promises:

    class Student(Person):
        """ An enrolled student at this institution. """

        def study(self, subject):
            raise NotImplementedError

    class LectureAttendee(Student):
        """ Someone who comes to lectures. """

        def attend(self, lecture):

    class StudentWhoActuallyStudies(Student):
        """ A student who actually is capable of studying. """

        def study(self, subject):
            """ Actually apply my knowledge of how to study. """

    alice = StudentWhoActuallyStudies("Alice")
    bob = Student("Bob")"chemistry")  # actual study"garden gnome painting")  # not implemented!

Now both Alice and Bob fulfil the technical requirements of a student at
the institution, but the expectations of study capability are clear.

Any code using this implementation of Student knows that, if they want a
student who actually studies, they'd better ask for a more specific

See? We can have overstretched analogies *and* remain within the Liskov
substitution principle.

 \       Eccles: “I just saw the Earth through the clouds!”  Lew: “Did |
  `\      it look round?”  Eccles: “Yes, but I don't think it saw me.” |
_o__)                            —The Goon Show, _Wings Over Dagenham_ |
Ben Finney

More information about the Python-list mailing list