Can Skim Section
This oddly placed section explains the type-instance and supertype-subtype relationships, and can be safely skipped if the reader is already familiar with these OO concepts. Skimming over the rules below might be useful though.
While we introduce many different objects, we only use two kinds of relationships (Figure 4.1, “Relationships”):
is a kind of (solid line): Known to the OO folks as specialization, this relationship exists between two objects when one (the subclass) is a specialized version of the other (the superclass). A snake is a kind of reptile. It has all the traits of a reptile and some specific traits which identify a snake.
Terms used: subclass of, superclass of and superclass-subclass.
is an instance of (dashed line): Also known as instantiation, this relationship exists between two objects when one (the instance) is a concrete example of what the other specifies (the type). I have a pet snake named Squasher. Squasher is an instance of a snake.
Terms used: instance of, type of, type-instance and class-instance.
Note that in plain English, the term 'is a' is used for both of the above relationships. Squasher is a snake and snake is a reptile are both correct. We, however, use specific terms from above to avoid any confusion.
We use the solid line for the first relationship because these objects are closer to each other than ones related by the second. To illustrate - if one is asked to list words similar to 'snake', one is likely to come up with 'reptile'. However, when asked to list words similar to 'Squasher', one is unlikely to say 'snake'.
It is useful at this point to note the following (independent) properties of relationships:
If X is an instance of A, and A is a subclass of B, then X is an instance of B as well.
If B is an instance of M, and A is a subclass of B, then A is an instance of M as well.
In other words, the head end of a dashed arrow can move up a solid arrow, and the tail end can move down (shown as 2a and 2b in Figure 4.2, “Transitivity of Relationships” respectively). These properties can be directly derived from the definition of the superclass-subclass relationship.
Applying Dashed Arrow Up Rule, we can derive the second statement from the first:
Squasher is an instance of snake (or, the type of Squasher is snake).
Squasher is an instance of reptile (or, the type of Squasher is reptile).
Earlier we said that an object has exactly one type. So how does Squasher have two? Note that although both statements are correct, one is more correct (and in fact subsumes the other). In other words:
snake. (In Python, the
__class__attribute points to the type of an object).
isinstance(Squasher, reptile)are true.
A similar rules exists for the superclass-subclass relationship.
If A is a subclass of B, and B is a subclass of C, then A is a subclass of C as well.
A snake is a kind of reptile, and a reptile is a kind of animal. Therefore a snake is a kind of animal. Or, in Pythonese:
__bases__attribute points to a tuple containing superclasses of an object).
issubclass(snake, animal)are true.
Note that it is possible for an object to have more than one base.