Inheritance and Subclassing
In this simple example, everything works without a hitch. The
country field of
Capital is added after the three original fields in
Position. Things get a little more complicated if any fields in the base class have default values.
This code will generate a
TypeError complaining that
non-default argument 'country' follows default argument. The problem is that our new
country field has no default value, while the
lat fields have default values.
01:55 In other words, if a field in a base class has a default value, then all new fields added in a subclass must have default values as well. Another thing to be aware of is how fields are ordered in a subclass.
Starting with the base class, fields are ordered in the order in which they are first defined. If a field is redefined in a subclass, its order doesn’t change. For example, if you define
Capital as seen onscreen,
Become a Member to join the conversation.