Pass teh object
d
to the functions
__str__
and
next
.
""" Pass an object to a function. """ import sys class Date(object): "A class Date with 3 properties (year, month, day). Pass an instance to a function." def __init__(self, month, day, year): "If the arguments are valid, create an instance of class Date." if not isinstance(year, int): raise TypeError("year must be an int") if not isinstance(month, int): raise TypeError("month must be an int") if month < 1 or month > 12: raise ValueError("month must be in range 1 to 12 inclusive") if not isinstance(day, int): raise TypeError("day must be an int") if day < 1 or day > 30: #Assume (incorrectly) that every month is 30 days. raise ValueError("day must be in range 1 to 30 inclusive") self.year = year self.month = month self.day = day def __str__(theDate): "Return a str showing all the information in theDate." return f"{theDate.month}/{theDate.day}/{theDate.year}" def next(theDate): "Move the value of theDate 1 day into the future." if theDate.day < 30: #Assume (incorrectly) that every month is 30 days. theDate.day += 1 else: theDate.day = 1 #Go to the first day of the next month. if theDate.month <= 12: theDate.month += 1 else: theDate.month = 1 #Go to the first month of the next year. theDate.year += 1 try: d = Date(7, 12, 1955) except BaseException as error: print(error, file = sys.stderr) sys.exit(1) print(f"d = {__str__(d)}") next(d) #Will change the value of d to be the next date. print(f"d = {__str__(d)}") sys.exit(0)
d = 7/12/1955 d = 7/13/1955