C W <tmrs...@gmail.com> writes:
> 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'
> clock = Clock('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?
Inside a method, you must explicitely use the instance
(conventionally (and in your case) names "self") to access an object
attribute. In your example, this means, you must use "self.time = '6:30'
not "time" alone (you did this in the "__init__" method).
If you are using "time" (without the "self." prefix), you are defining
a new local variable.
In this regard, Python differs from many object oriented languages.