Relationships

Can Skim Section

This 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.

While we introduce many different objects, we only use two kinds of relationships (Figure 1.2, “Relationships”):

  • is a kind of (solid line): Known to the OO folks as specialization, this relationship exists between two objects when one (the subtype) is a specialized version of the other (the supertype). 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: subtype of, supertype of and supertype-subtype.

  • 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 and type-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.

Figure 1.2. Relationships

Relationships

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:

Dashed Arrow Up Rule

If X is an instance of A, and A is a subtype of B, then X is an instance of B as well.

Dashed Arrow Down Rule

If B is an instance of M, and A is a subtype 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 1.3, “Transitivity of Relationships” respectively). These properties can be directly derived from the definition of the supertype-subtype relationship.

Figure 1.3. Transitivity of Relationships

Transitivity of Relationships

Applying Dashed Arrow Up Rule, we can derive the second statement from the first:

  1. Squasher is an instance of snake (or, the type of Squasher is snake).

  2. 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:

  • Squasher.__class__ is snake. (In Python, the __class__ attribute points to the type of an object).

  • Both isinstance(Squasher, snake) and isinstance(Squasher, reptile) are true.

A similar rules exists for the supertype-subtype relationship.

Combine Solid Arrows Rule

If A is a subtype of B, and B is a subtype of C, then A is a subtype 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:

  • snake.__bases__ is (reptile,). (The __bases__ attribute points to a tuple containing supertypes of an object).

  • Both issubclass(snake, reptile) and issubclass(snake, animal) are true.

Note that it is possible for an object to have more than one base.