I have a class that I defined in the following way:
class Time:
def print_time(self, am_pm):
if am_pm == 'AM':
tot_seconds = self.hour * 3600 + self.minute * 60 + self.second
else:
... tot_seconds = self.hour * 3600 + self.minute * 60 + self.second + 12*3600.0
print('%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second))
print('Seconds passed since midnight: ', tot_seconds)
I create an instance of Time with start = Time and then I specify the instance attributes as start.hour = 10, start.minute = 5, start.second = 2.
When I call the method print_time, if I do Time.print_time(start, 'AM') then I get the correct output. But if I try to pass start as self it does not work:
start.print_time('AM')
Traceback (most recent call last):
File "<ipython-input-68-082a987f3007>", line 1, in <module>
start.print_time('PM')
TypeError: print_time() missing 1 required positional argument: 'am_pm'
But why it is so? I thought that start would be the subject of the method invocation and so it would count as the first parameter and so I would need to specify only the am_pm parameter in print_time(). Why is this wrong?
start = Timetostart = Time().Time.print_time(start, 'AM')works coincidentally,startis not actually an instance ofTimehere.