Concepts / The __init__ Method and Instance Variables

The __init__ Method and Instance Variables

A child class extends a parent class using the syntax class ChildClass(ParentClass):, inheriting all parent variables and methods automatically.

  • Programming

From Parent to Child

Inheritance lets one class extend another class. The existing class is the parent class, and the new class is the child class. A child class inherits the parent class's variables and methods, then can add variables and methods of its own.

Definition

The syntax class ChildClass(ParentClass): defines ChildClass as a child of ParentClass. The parent-child relationship is one-way: the child inherits from the parent, but the parent does not inherit from the child.

ParentClassparent classChildClasschild classinherits from
How does inheritance connect a child class to its parent?

What the Child Receives

A child class automatically inherits all variables and methods from its parent. This means an instance of the child class can use the parent's variables and methods without redefining them in the child class.

Worked example

PartyAnimal and CricketFan

Determine which features belong to the parent class and which features are added by the child class.

  1. 1
    Identify the parent

    PartyAnimal is the parent class in the example.

  2. 2
    Identify the child

    CricketFan is the child class. Its inheritance syntax is class CricketFan(PartyAnimal):.

  3. 3
    List inherited features

    CricketFan inherits the name variable and the party() method from PartyAnimal.

  4. 4
    List added features

    CricketFan adds the points variable and the six() method.

  5. 5
    Inspect the instance

    An instance named j has access to name, party(), points, and six().

Result

The child instance combines inherited functionality from PartyAnimal with new functionality defined by CricketFan.

PartyAnimalparent classnameinherited variablepointschild variablejchild-class instanceparty()inherited methodCricketFanchild classsix()child methoddefinesdefinesdefinesdefinesparent ofcreatesfeatures inherited by
Which variables and methods come from the parent, and which are defined directly by the child?

Initialization with super()

A child class often needs to initialize two sets of variables: the variables belonging to the parent and new variables belonging to the child. The child class can define its own __init__ method and use super().__init__() inside it to call the parent's __init__ method.

Best practice

Call super().__init__() before setting up child-specific variables. This ensures that the parent class completes its required initialization first, after which the child can add its own setup.

What do you think happens?

When a child object's __init__ method uses super().__init__(), which initialization happens first?

  • The parent class's initialization

  • The child-specific initialization

  • Neither initialization

Reveal answer
Answer:

The parent class's initialization

super().__init__() calls the parent class's __init__ method. The child then sets up its own variables after the parent setup is complete.

Create child objectChild __init__super().__init__()Parent __init__Child variablesInitialized objectstartscallstransfers control toreturns beforecompletes
What happens during child-object initialization, and how does control move from the child __init__ method to the parent __init__ method?

A Combined Instance

Imagine creating an instance j of CricketFan. During initialization, the parent setup provides the inherited name variable. The child setup provides the points variable. The resulting instance has access to both variables, as well as the inherited party() method and the child-defined six() method.

jCricketFan instancenameinherited variablepointschild variableparty()inherited methodsix()child methodhas access tohas access tohas access tohas access to
Which attributes belong to the particular child-class instance after initialization?

Extending the Parent

A child class extends a parent class in two directions. First, it receives the parent's existing variables and methods through inheritance. Second, it contributes new variables and methods that make the child more specialized. In the source example, CricketFan keeps name and party() from PartyAnimal and adds points and six().

PartyAnimalnamenameparty()CricketFanparty()pointssix()definesdefinesinheritsinheritsaddsadds
What functionality does the child keep from the parent, and what new functionality does it add?

Common Initialization Mistakes

  • Common mistake

    Defining the child class without naming the parent in parentheses

    What it looks like

    Using a class definition that does not follow the pattern class ChildClass(ParentClass):

    Why it fails

    The parent-child inheritance relationship is communicated by placing the parent class name in parentheses after the child class name.

    Do this instead

    Use the inheritance syntax class ChildClass(ParentClass):.

  • Common mistake

    Initializing only the child-specific variables

    What it looks like

    The child __init__ method sets up points but does not call super().__init__().

    Why it fails

    The parent class's required setup is not completed.

    Do this instead

    Call super().__init__() from the child __init__ method before setting up child-specific variables.

  • Common mistake

    Assuming the parent receives features from the child

    What it looks like

    Expecting PartyAnimal to gain CricketFan's points variable or six() method.

    Why it fails

    Inheritance is one-way from parent to child.

    Do this instead

    Remember that CricketFan inherits from PartyAnimal; PartyAnimal does not inherit from CricketFan.

  • Common mistake

    Forgetting that inherited methods remain available

    What it looks like

    Treating a CricketFan instance as if it can use only six() and not party().

    Why it fails

    A child instance has access to the parent's inherited methods as well as its own methods.

    Do this instead

    Include both inherited and child-defined functionality when identifying what the instance can access.

Practice the Trace

MEDIUM

A parent class provides the variable title and the method describe(). A child class extends it by adding the variable level and the method advance(). Trace the child object's initialization: identify which initialization must happen through super().__init__(), then list the inherited and child-defined features available on the finished object.

Hints
  • The parent variables and methods are inherited by the child.
  • The child should call super().__init__() before setting up level.
  • Separate inherited features from features defined directly in the child.
  1. 1
    To solve the trace, begin with the parent initialization through super().__init__(). Then add the child variable level. The finished child object has title and describe() from the parent, plus level and advance() from the child.

Key Takeaways

  • Define inheritance with the pattern class ChildClass(ParentClass):.
  • A child class inherits the parent's variables and methods automatically.
  • Use super().__init__() inside the child __init__ method to complete parent initialization.
  • Call super().__init__() before setting child-specific variables.
  • A child instance combines inherited features with the new variables and methods defined by the child.

Related concepts

More concepts starting with T