Design by contract in Python - preconditions


Python Clean Code Tip:

Use preconditions to ensure the integrity of your objects.

For example:

class Date:
    def __init__(self, day, month, year):
        self.day = day
        self.month = month
        self.year = year


startDate = Date(3, 11, 2020)
# OK

startDate = Date(31, 13, 2020)
# this one should fail since there are only 12 months


class Date:
    LAST_MONTH = 12
    LAST_DAY = 31

    def __init__(self, day, month, year):
        if month > self.LAST_MONTH:
            raise Exception(f"Month cannot be greater than {self.LAST_MONTH}")
        if day > self.LAST_DAY:
            raise Exception(f"Day cannot be greater than {self.LAST_DAY}")
        self.day = day
        self.month = month
        self.year = year


startDate = Date(3, 11, 2020)
# OK

startDate = Date(31, 13, 2020)
# this one fails


# DISCLAIMER: production ready validation should be more complex since not all months have 31 days