class Time(object): def __init__(self, hour, min): self.hour = hour self.min = min def __str__(self): """overloading the str operator (STRing)""" return "[ {:2}:{:02} ]".format(self.hour, self.min) def __repr__(self): """overloading the repr operator (REPResentation)""" return "Time(%2d,%2d)" % (self.hour, self.min) def __gt__(self, other): """overloading the GreaTer operator""" selfminutes = self.hour * 60 + self.min otherminutes = other.hour * 60 + other.min if selfminutes > otherminutes: return True else: return False def __add__(self, other): hour = self.hour + other.hour minute = self.min + other.min while minute > 60: minute = minute - 60 hour = hour + 1 while hour > 24: hour = hour - 24 return self.__class__(hour, minute) class TimeUK(Time): """Derived (or inherited class)""" def __str__(self): """overloading the str operator (STRing)""" if self.hour < 12: ampm = "am" else: ampm = "pm" print("Time representation = {}".format(Time.__str__(self))) return "[{:2}:{:02}{}]".format(self.hour % 12, self.min, ampm) if __name__ == "__main__": t1 = Time(11, 45) t2 = Time(10, 20) print("The sum of t1 and t2 is") t3 = t1 + t2 print(t3)