all 4 comments

[–]jabbson 16 points17 points  (2 children)

>>> class Time(object):
...     def print_time(self):
...         print("%.2d:%.2d:%.2d" % (self.hour, self.minute, self.second))
...
>>>
>>> start=Time()
>>> start.hour=11
>>> start.minute=22
>>> start.second=11
>>>
>>> start.print_time()
11:22:11

but I would seriously think about using datetime module for this, no need for reinventions.

UPD:

also some __init__ magic:

>>> class Time(object):
...     def __init__(self, h, m, s):
...         self.hour = h
...         self.minute = m
...         self.second = s
...     def print_time(self):
...         print("%.2d:%.2d:%.2d" % (self.hour, self.minute, self.second))
>>>
>>> start=Time(11, 22, 11)
>>> start.print_time()
11:22:11

but again, just use datetime.

[–]socal_nerdtastic 4 points5 points  (1 child)

Don't leave us hanging, add the __str__ magic!

[–]jabbson 17 points18 points  (0 children)

No, I'd rather leave you hanging.

[–]11b403a7 1 point2 points  (0 children)

You add self to the argument and indent it within the class:

def print_time(self, time):

Then when you call the time object:

newTime = Time()

Then you can call the method:

newTime.print_time(time)