osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Which part of the loop is it going through in this class frame?


Hello,

I am new to OOP. I'm a bit confused about the following code.

class Clock(object):
    def __init__(self, time):
        self.time = time
    def print_time(self):
        time = '6:30'
        print(self.time)

clock = Clock('5:30')
clock.print_time()
5:30

I set time to 6:30, but it's coming out to 5:30. I guess it's because I
passed in 5:30, so, it's replaced?

How does line-by-line execution run inside a frame? How does __init__ work?
I understand you must have __init__. Is it run before print_time(), if so,
why don't I just set self.time = '6:30' instead of self.time = time?

Thanks!