Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module functions

Static methods might be better off placed in a module as simple functions.

def is_valid_date(year, month, day):
    if 0 <= year <= 3000 and  1 <= month <= 12 and 1 <= day <= 31:
        return True
    else:
        return False

class Date(object):
    def __init__(self, Year, Month, Day):
        if not is_valid_date(Year, Month, Day):
            raise Exception('Invalid date')
        self.year  = Year
        self.month = Month
        self.day   = Day

    def __str__(self):
        return 'Date({}, {}, {})'.format(self.year, self.month, self.day)


import mydate

a = mydate.Date(2013, 10, 20)
print(a)

print(mydate.is_valid_date(2013, 10, 40))

b = mydate.Date(2013, 13, 20)

Date(2013, 10, 20)
False
Traceback (most recent call last):
  File "run.py", line 8, in <module>
    b = mydate.Date(2013, 13, 20)
  File "module_function/mydate.py", line 10, in __init__
    raise Exception('Invalid date')
Exception: Invalid date