An inner class or nested class is a defined entirely within the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have more than one inner classes, but in general inner classes are avoided.
Complete Python Bootcamp: Go from zero to hero in Python 3
Inner class example
We create a class (Human) with one inner class (Head).
An instance is created that calls a method in the inner class:
In the program above we have the inner class Head() which has its own method. An inner class can have both methods and variables. In this example the constructor of the class Human (init) creates a new head object.
Multiple inner classes
You are by no means limited to the number of inner classes, for example this code will work too:
By using inner classes you can make your code even more object orientated. A single object can hold several sub objects. We can use them to add more structure to our programs.