class Date: """ A user-defined data structure that stores and manipulates dates """ def __init__(self, month, day, year): """ the constructor for objects of type Date """ self.month = month self.day = day self.year = year def __repr__(self): """ Returns a string representation for the object of type Date that calls it (named self). ** Note that this _can_ be called explicitly, but it more often is used implicitly via the print statement or simply by expressing self's value. """ return "{:02d}/{:02d}/{:04d}".format(self.month, self.day, self.year) def __eq__(self, other): ''' Method that overloads the equal sign '==' ''' if self.year == other.year and \ self.month == other.month and \ self.day == other.day: return True else: return False def __gt__(self, other): ''' Method that overloads the greater-than sign '>' ''' return self.is_after(other) def __lt__(self, other): ''' Method that overloads the less-than sign '<' ''' return self.is_before(other) # def __ne__(self, other): ''' print('in ne') return not(self == other) ''' # pass def __ge__(self, other): ''' Method that overloads the great-than-equal sign '>=' ''' return self.is_after(other) or self == other def is_equal(self, date2): """ Decides if self and date2 represent the same calendar date, whether or not they are in the same place in memory. """ return self.year == date2.year and self.month == date2.month \ and self.day == date2.day def is_before(self, date): """ Determines if self occurs before date. """ if self.year != date.year: return self.year < date.year elif self.month != date.month: return self.month < date.month else: return self.day < date.day def is_after(self, date): """ Determines if self occurs after date. """ if self.is_equal(date): return False else: return not self.is_before(date) def is_leap_year(self): """ Returns True if the calling object is in a leap year; False otherwise. """ if self.year % 400 == 0: return True elif self.year % 100 == 0: return False elif self.year % 4 == 0: return True return False import doctest doctest.testmod() d = Date(4, 5, 2016) print(d) d1 = Date(4, 6, 2016) print(d1) if d < d1: print(str(d) + ' < ' + str(d1)) elif d > d1: print(str(d) + ' > ' + str(d1)) else: print(str(d) + ' == ' + str(d1)) print('str form ' + str(d)) print('repr form ' + repr(d))