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

Dan Sommers dan at tombstonezero.net
Wed Mar 7 20:28:31 EST 2018


On Wed, 07 Mar 2018 16:57:51 -0500, C W wrote:

> 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?

Here's a hint:  within a method, time and self.time are not the same (at
least not in Python; other languages may (and do) work differently).

Dan




More information about the Python-list mailing list