Inner Class in Python

11 May, 2022
Python is an Object-Oriented Programming Language, everything in python is related to objects, methods, and properties. A class is a user-defined blueprint or a prototype, which we can use to create the objects of a class. The class is defined by using the class keyword.
Example of class
Python3
# create a Geeksforgeeks classclass Geeksforgeeks :   gfg = 10
First of all, we have to understand the __init__() built-in method for understanding the meaning of classes. Whenever the class is being initiated, a method namely __init__() is always executed. An __init__() method is used to assign the values to object properties or to perform the other method that is required to complete when the object is created.
Example: class with __init__() method
Python3
# create a Geeksforgeeks classclass Geeksforgeeks:  # constructor method def __init__(self):   # object attributes  self.course = "Campus preparation"  self.duration = "2 months"   # define a show method # for printing the content def show(self):  print("Course:", self.course)  print("Duration:", self.duration) # create Geeksforgeeks# class objectouter = Geeksforgeeks() # method callingouter.show()
Output:
Course : Campus Preparation
Duration : As per your schedule 
A class defined in another class is known as an inner class or nested class. If an object is created using child class means inner class then the object can also be used by parent class or root class. A parent class can have one or more inner classes but generally inner classes are avoided.
We can make our code even more object-oriented by using an inner class. A single object of the class can hold multiple sub-objects. We can use multiple sub-objects to give a good structure to our program.
Example:
First, we create a class and then the constructor of the class.
After creating a class, we will create another class within that class, the class inside another class will be called an inner class.
Python3
# create a Color classclass Color:     # constructor method  def __init__(self):    # object attributes    self.name = 'Green'    self.lg = self.Lightgreen()     def show(self):    print("Name:", self.name)     # create Lightgreen class  class Lightgreen:     def __init__(self):        self.name = 'Light Green'        self.code = '024avc'        def display(self):        print("Name:", self.name)        print("Code:", self.code) # create Color class objectouter = Color() # method callingouter.show() # create a Lightgreen# inner class objectg = outer.lg # inner class method callingg.display()
Output:
 Green
Name:Green

Light Green
023gfd

Name: Light Green
Code: 023gfd
For the grouping of two or more classes. Suppose we have two classes remote and battery. Every remote needs a battery but a battery without a remote won’t be used. So, we make the Battery an inner class to the Remote. It helps us to save code. With the help of the inner class or nested class, we can hide the inner class from the outside world. Hence, Hiding the code is another good feature of the inner class. By using the inner class, we can easily understand the classes because the classes are closely related. We do not need to search for classes in the whole code, they all are almost together. Though inner or nested classes are not used widely in Python it will be a better feature to implement code because it is straightforward to organize when we use inner class or nested class.
Syntax:
# create NameOfOuterClass class
class NameOfOuterClass:

  # Constructor method of outer class
  def __init__(self):  

    self.NameOfVariable = Value

    # create Inner class object
    self.NameOfInnerClassObject = self.NameOfInnerClass() 
 
  # create a NameOfInnerClass class
  class NameOfInnerClass:  
     # Constructor method of inner class
    def __init__(self): 
      self.NameOfVariable = Value

# create object of outer class
outer = NameOfOuterClass() 
Types of inner classes are as follows: 
Multiple inner classMultilevel inner class
Multiple inner class
Multilevel inner class
The class contains one or more inner classes known as multiple inner classes. We can have multiple inner class in a class, it is easy to implement multiple inner classes. 
Example: Multiple inner class
Python3
# create outer classclass Doctors:    def __init__(self):        self.name = 'Doctor'        self.den = self.Dentist()        self.car = self.Cardiologist()     def show(self):        print('In outer class')        print('Name:', self.name)     # create a 1st Inner class    class Dentist:        def __init__(self):            self.name = 'Dr. Savita'            self.degree = 'BDS'        def display(self):            print("Name:", self.name)            print("Degree:", self.degree)     # create a 2nd Inner class    class Cardiologist:        def __init__(self):            self.name = 'Dr. Amit'            self.degree = 'DM'        def display(self):            print("Name:", self.name)            print("Degree:", self.degree) # create a object# of outer classouter = Doctors()outer.show() # create a object# of 1st inner classd1 = outer.den # create a object# of 2nd inner classd2 = outer.carprint()d1.display()print()d2.display()
Output:
In outer class
Name: Doctor

In inner class 1
Name: Dr. Savita
Degree: BDS

In inner class 2
Name: Dr. Amit
Degree: DM 
The class contains an inner class and that inner class again contains another inner class, this hierarchy is known as the multilevel inner class.
Example: Multilevel inner class
Python3
# create a outer classclass Geeksforgeeks:      def __init__(gfg):     # create a inner class object     self.inner = self.Inner()   def show(gfg):     print('This is an outer class')   # create a 1st inner class  class Inner:     def __init__(self):     # create a inner class of inner class object     self.innerclassofinner = self.Innerclassofinner()      def show(self):    print('This is the inner class')      # create a inner class of inner   class Innerclassofinner:                def show(self):         print()                      def show(self):         print('This is an inner class of inner class') # create a outer class object# i.e.Geeksforgeeks class objectouter = Geeksforgeeks()outer.show()print() # create a inner class objectgfg1 = outer.innergfg1.show()print() # create a inner class of inner class objectgfg2 = outer.inner.innerclassofinnergfg2.show()
Output:
This is an outer class This is an inner class This is an inner class of inner class means multilevel inner class
shiprarathore17
Python-OOP
python-oop-concepts
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate through Excel rows in Python?
Python Dictionary
Python OOPs Concepts
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Python Classes and Objects
Introduction To PYTHON
Stack in Python
Queue in Python
Read a file line by line in Python