4.11 Inheritance As Classification
A common use for inheritance is the hierarchical classification of objects. Suppose there is one superclass that has many subclasses. Since all the subclasses have the same operations as their superclass, they can be used wherever the superclass is expected. The superclass, then, specifies what is common to all the subclasses, and the subclasses can indicate how they differ from the common characteristics. The superclass specifies the general kind of thing, and the subclasses specify variations. This fits in with how we define things. Actually, there are two common ways that we define things: using an abstract, Aristotelian definition or using a definition by reference to an example.
In an Aristotelian definition, we define a thing by the category of thing that it is and then by how it differs from the other things in that category. Using an Aristotelean definition, the superclass is the category of thing, and the methods and attributes in the subclass specify the differences from the category.
When you are using inheritance in this Aristotelian sense, the superclass is often an abstract class. An abstract class is something like a "bird," whereas the subclasses will be things like robins, penguins, and ostriches. An abstract class is not intended to be used itself to create objects, only to group together its subclasses, just as there is no instance of "bird" that is not some particular kind of bird.
When you implement an abstract class in Python, you often do not provide implementations for all the methods shared by members of the subclasses. Some methods have no behavior that is common to all instances.
AbstractSet, Figure 45, is an example of an abstract class. The superclass provides an interface that can have several implementations. The algorithms that use the objects don't need to know the implementation; they only need to know the interface. There are seven methods that are not defined in AbstractSet, but only in subclasses.
In other object-oriented languages like Java, AbstractSet would have to provide method signatures for the methods that are to be provided by the subclasses. Method signatures give the name of the method, the number and types of parameters, and the result type. The compiler needs this information to be able to compile calls to the methods.
In Python, there are no parameter types or result types to put in signatures, and methods are looked up at run-time. We did not have to put defs in AbstractSet for the seven methods. We did, however, put them in and made them all raise a NotImplementedError exception. If an instance of AbstractSet itself is created, a NotImplementedError will be raised when it is first used. If a subclass is coded without all the required methods, it will be raised as soon as a missing method is called. NotImplementedError was invented for precisely this purpose. It allows you to put defs for the required methods in the superclass, which is good for documentation, and it gives a more precise error message than that the attribute was not found. For an example of the error messages, here we create an instance of AbstractSet and try to call the copy() method and then to call a nonexistent remove() method.
>>> import AbstractSet >>> x=AbstractSet.AbstractSet() >>> x.copy() Traceback (most recent call last): File "<stdin>", line 1, in ? File "AbstractSet.py", line 9, in copy def copy(self): raise NotImplementedError,"set.copy()" NotImplementedError: set.copy() >>> x.remove(1) Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'AbstractSet' instance has no attribute 'remove'
So, if you are defining classes the way Aristotle suggested, you have an abstract superclass and you create concrete subclasses of it"concrete" meaning that all the details are filled in, all the methods are defined. But that's not the way we usually understand things. Mentally, we usually use a paradigm, an example instance, and relate other things to it. For example, most people in the U.S. seem to use the robin as a paradigmatic bird. Other birds are considered birds because they resemble robins: feathers, beaks, wings, nests, eggs, flying, and soon. What about penguins and ostriches? Well, they are a lot like robinsfeathers, wings, beaksbut penguins swim instead of flying and they aren't big on nests. Ostriches run.
When you program using a paradigmatic definition, you use a concrete superclass that represents the example object, and concrete subclasses that represent the related things.
Would that have worked with ListSet and DictSet?
If we made ListSet the paradigm, it would try to have its own list, and then the DictSet would override that with its own dictionary. If we used different attribute names for the data structures, then each instance of a DictSet would have both a list and a dictionary. But we programmed both of them to use the attribute name rep for their data structures. That would save space in DictSet, since it could override ListSet's list with its own dictionary.
But is that safe? ListSet contains code that assumes it's manipulating a list. If any of that code is executed, the program will crash. So DictSet would have to override all of ListSet's list manipulation code. We wrote ListSet and DictSet in such a way that that could happen. All the data structure specific code is in separate methods that can be overridden. If we had been writing ListSet in isolation, would we have done that? Would we have been so careful? Probably not. And if we weren't careful, we would have to override all the methods, rather than being able to share code for union and intersection and the others. And even if there were a few methods in ListSet that didn't need to be overridden, would it be safe to use them? If someone changed the implementation of ListSet, it could break our code. In this case, at least, where we are providing different implementations, an AbstractSet class, designed to be overridden, is much better choice.
In Python, we can get around all this discussion of whether to inherit from an abstract superclass or a concrete one. We do not have to inherit at all. All we have to do is provide classes with the proper interface, the proper collection of methods. To adapt an old saying, if it looks like a duck and walks like a duck and quacks like a duck, I don't care whether it "really" is a duck: I'll treat it like a duck.