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

26.1.2. Instantiation of an old-style class: The constructor, .__init__()

The creation of a new instance of a class happens when a running Python program encounters a call to that class, that is, the class name with a pair of parentheses after it, with zero or more arguments inside the parentheses.

Here is the general form:

C(a1, a2, ...)                

The instance creation (also called instantiation) is handled by the __init__() or constructor method.

  1. First Python creates the instance with an empty .__dict__ attribute that will contain the instance's values.

  2. Python then calls the constructor. The argument list for this call always has the special first argument self (the instance), followed by whatever arguments were used in the initial call. The constructor call is equivalent to this:

    C.__init__(self, a1, a2, ...)
    

  3. The constructor method then executes. Typically the constructor will set up new instance attributes by assignments of this form:

            self.name = expression
    

    When the constructor finishes executing, the instance is returned to the constructor's caller.