I learn Classes and methods according to a manual "Think Python: How to Think Like a Computer Scientist"
I am stuck on "Polymorphism" There is a part using built-in function sum.
I don't know what to do... It should be connected to __add__ function, but still the code must be somhow changed. If I use just total=t1+t2+t3 it works.
Here is a link to a web manual:
here is a code:
def int_to_time(seconds): time = Time() minutes, time.second = divmod(seconds, 60) time.hour, time.minute = divmod(minutes, 60) return time class Time(object): def __init__(self, hour=0, minute=0, second=0): self.hour = hour self.minute = minute self.second = second def __add__(self, other): seconds = self.time_to_int() + other.time_to_int() return int_to_time(seconds) def time_to_int(self): minutes = self.hour * 60 + self.minute seconds = minutes * 60 + self.second print seconds return seconds def __str__(self): return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second) def main(): t1 = Time(7, 43) t2 = Time(7, 41) t3 = Time(7, 37) total = sum([t1, t2, t3]) print total if __name__ == '__main__': main()
I really approciate your help guys. Many thanks!