Which part of the loop is it going through in this class frame?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Mar 8 23:28:48 EST 2018
On Thu, 08 Mar 2018 20:25:42 -0500, C W wrote:
> Thank you guys, lots of great answers, very helpful. I got it!
>
> A follow-up question:
>
> How did the value of "object" get passed to "time"? Obviously, they have
> different names. How did Python make that connection?
It didn't. You have misunderstood what is happening. Let's go though it
bit by bit:
> Code is below for convenience.
>
> class Clock(object):
The "class" statement declares a new class, called "Clock", which
inherits from the built-in class "object".
[Aside: some people describe "object" as a "type" rather than a class.
There are some differences in meaning between type/class in computer
science, and in Python 2 they are slightly different things, but in
Python 3 you can consider class and type to be synonyms.]
So object is not a parameter, it is a superclass. This tells Python that
your Clock class is a subclass of object.
> def __init__(self, time):
> self.time = time
The initialiser __init__ method is special, because it defines the
signature for calling the class. So when you say:
clock = Clock("5:30")
Python creates a new Clock instance, and calls __init__ and passes "5:30"
as the *time* parameter.
(I have glossed over some technical details and complexities.)
The thing to remember is that when you create a new instance by calling
the class Clock(...), your arguments have to match the __init__ method.
--
Steve
More information about the Python-list
mailing list