Inheritance
We are going to create a simple example that uses inheritance.
Class Driver
is the base (abstract) class that represent a generic Driver. It
will contain the one common attribute, one method concrete and one abstract.
Concrete methods are methods that can be called from the base class, and abstract methods are methods that needs to be implemented in the subclass.
src.intermediate.inheritance.inheritance.Driver
dataclass
Bases: ABC
This is a driver, an abstract class that represents a driver.
Source code in src/intermediate/inheritance/inheritance.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
src.intermediate.inheritance.inheritance.Driver.is_novel()
Return whatever the driver is novel or not.
Returns:
Name | Type | Description |
---|---|---|
is_novel |
bool
|
True if the driver is novel, False otherwise. |
Source code in src/intermediate/inheritance/inheritance.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
src.intermediate.inheritance.inheritance.Driver.speed_limit()
abstractmethod
Return the speed limit with metic unit for the driver.
It is an abstract method, must be implemented by subclasses.
Returns:
Name | Type | Description |
---|---|---|
speed_limit |
str
|
speed limit with metric unit for driver. |
Source code in src/intermediate/inheritance/inheritance.py
36 37 38 39 40 41 42 43 44 45 |
|
Subclass UsaDriver represents a Driver in USA. It defines the novel years and
the metric unit for the speed. Abstract method speed_limit
is implemented.
src.intermediate.inheritance.inheritance.UsaDriver
dataclass
Bases: Driver
Concrete class of a Driver that can drive in USA.
Source code in src/intermediate/inheritance/inheritance.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
src.intermediate.inheritance.inheritance.UsaDriver.speed_limit()
Return the speed limit with metic unit for the driver.
Returns:
Name | Type | Description |
---|---|---|
speed_limit |
str
|
speed limit with metric unit for driver. |
Source code in src/intermediate/inheritance/inheritance.py
55 56 57 58 59 60 61 |
|
Subclass SpainDriver represents a Driver in Spain. It defines the novel years
and the metric unit for the speed. Abstract method speed_limit
is implemented.
src.intermediate.inheritance.inheritance.SpainDriver
dataclass
Bases: Driver
Concrete class of a Driver that can drive in Spain.
Source code in src/intermediate/inheritance/inheritance.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
src.intermediate.inheritance.inheritance.SpainDriver.speed_limit()
Return the speed limit with metic unit for the driver.
Returns:
Name | Type | Description |
---|---|---|
speed_limit |
str
|
speed limit with metric unit for driver. |
Source code in src/intermediate/inheritance/inheritance.py
71 72 73 74 75 76 77 78 79 |
|