Next / Previous / Contents / TCC Help System / NM Tech homepage

11.8. Life cycle of an instance

To really understand what is going on inside a running Python program, let's follow the creation of an instance of the SnailRun class from the preceding section.

Just for review, let's assume you are using conversational mode, and you create a variable like this:

>>> badPi = 3.00

Whenever you start up Python, it creates the “global namespace” to hold the names and values you define. After the statement above, here's how it looks.

Next, suppose you type in the class definition as above. As it happens, a class is a namespace too—it is a container for methods. So the global namespace now has two names in it: the variable badPi and the class SnailRun. Here is a picture of the world after you define the class:

Next, create an instance of class SnailRun like this:

>>> j1 = SnailRun ( 'Judy', 87.3 )

Here is the sequence of operations:

  1. Python creates a new instance namespace. This namespace is initially a copy of the class's namespace: it contains the two methods .__init__() and .show().

  2. The constructor method starts execution with these arguments:

    • The name self is bound to the instance namespace.

    • The name snailName is bound to the string value 'Judy'.

    • The name finishTime is bound to the float value 87.3.

  3. This statement in the constructor

            self.name  =  snailName
    

    creates a new attribute .name in the instance namespace, and assigns it the value 'Judy'.

  4. The next statement in the constructor creates an attribute named .time in the instance namespace, and binds it to the value 87.3.

  5. The constructor completes, and back in conversational mode, in the global namespace, variable j1 is bound to the instance namespace.

Here's a picture of the world after all this: