Skip to content

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
@dataclass
class Driver(ABC):
    """This is a driver, an abstract class that represents a driver."""

    license_valid_from: datetime.date = None

    def is_novel(self) -> bool:
        """Return whatever the driver is novel or not.

        Returns:
            is_novel: True if the driver is novel, False otherwise.
        """
        novel_days: int = 365 * self.novel_years
        novel_until: datetime.date = (
            self.license_valid_from + datetime.timedelta(days=novel_days)
        )
        return (
            novel_until
            >= datetime.datetime.now(
                tz=datetime.timezone.utc,
            ).date()
        )

    @abstractmethod
    def speed_limit(self) -> str:
        """Return the speed limit with metic unit for the driver.

        It is an abstract method, must be implemented by subclasses.

        Returns:
            speed_limit: speed limit with metric unit for driver.
        """
        raise NotImplementedError

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
def is_novel(self) -> bool:
    """Return whatever the driver is novel or not.

    Returns:
        is_novel: True if the driver is novel, False otherwise.
    """
    novel_days: int = 365 * self.novel_years
    novel_until: datetime.date = (
        self.license_valid_from + datetime.timedelta(days=novel_days)
    )
    return (
        novel_until
        >= datetime.datetime.now(
            tz=datetime.timezone.utc,
        ).date()
    )

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
@abstractmethod
def speed_limit(self) -> str:
    """Return the speed limit with metic unit for the driver.

    It is an abstract method, must be implemented by subclasses.

    Returns:
        speed_limit: speed limit with metric unit for driver.
    """
    raise NotImplementedError

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
@dataclass
class UsaDriver(Driver):
    """Concrete class of a Driver that can drive in USA."""

    novel_years = 2
    metric_unit = "mph"

    def speed_limit(self) -> str:
        """Return the speed limit with metic unit for the driver.

        Returns:
            speed_limit: speed limit with metric unit for driver.
        """
        return f"70{self.metric_unit}"

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
def speed_limit(self) -> str:
    """Return the speed limit with metic unit for the driver.

    Returns:
        speed_limit: speed limit with metric unit for driver.
    """
    return f"70{self.metric_unit}"

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
@dataclass
class SpainDriver(Driver):
    """Concrete class of a Driver that can drive in Spain."""

    novel_years = 1
    metric_unit = "km/h"

    def speed_limit(self) -> str:
        """Return the speed limit with metic unit for the driver.

        Returns:
            speed_limit: speed limit with metric unit for driver.
        """
        if self.is_novel():
            return f"100{self.metric_unit}"
        return f"120{self.metric_unit}"

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
def speed_limit(self) -> str:
    """Return the speed limit with metic unit for the driver.

    Returns:
        speed_limit: speed limit with metric unit for driver.
    """
    if self.is_novel():
        return f"100{self.metric_unit}"
    return f"120{self.metric_unit}"