"C W" wrote in message news:cae2fw2nudjcmvukavzh01trkqeentkdxdpbawcphhsgx8jv...@mail.gmail.com...

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?


In print_time(), the variable 'time' is not the same as the variable 'self.time'.

'time', a local variable, is set to '6:30'.

'self.time', an instance variable, is set to '5:30'.

You printed self.time, hence the result.

Frank Millman


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to