Software Design by Example in Python 2: Objects and Classes
Python is an object-oriented language, but many of its more advanced features rely on its support for introspection, i.e., on the ability of running programs to ask, “What do I contain?” In order to show newcomers how this works, Chapter 1: Objects and Classes shows learners that objects and classes are “just” dictionaries. In particular, it demonstrates that:
-
Objects and classes can be thought of as dictionaries with stereotyped behavior.
-
Objects can exist without classes, but classes make them easier to understand.
-
Objects that satisfy the same contract are polymorphic, i.e., they can be used interchangeably even if they do different specific things.
-
Inheritance can be implemented in several ways that differ in the order in which objects and classes are searched for methods.
By its end, this chapter has introduced the most important idea in the whole book, which is that programs are just another kind of data. Understanding this turns out to be key to many designs: from passing functions as parameters to loading modules dynamically or walking abstract syntax trees, it comes up in program after program.
Terms defined: alias, argument, cache, class method, constructor, derived class, design by contract, monkey patching, multiple inheritance, object-oriented programming, parameter, polymorphism, recursion, spread, static method, upcall, varargs.