Python Inner Classes: Understanding Nested Classes

Explore inner classes in Python, also known as nested classes, which are defined within another class. Learn how inner classes inherit attributes from the outer class and how they can be utilized within the parent class. Understand the syntax and practical applications of inner classes to enhance code organization and structure.



Python - Inner Classes

An inner class is a class defined inside another class. It's also known as a nested class. The inner class can be used by the parent class and becomes one of its attributes. Inner classes inherit the attributes of the outer class automatically.

Syntax


class outer:
   def __init__(self):
      pass
   class inner:
      def __init__(self):
         pass
        

Inner classes group related classes together, making the code easier to understand. The inner class has a local scope and acts as one of the attributes of the outer class.

Example

In this example, student is the outer class and subjects is the inner class. The outer class initializes the name attribute and an instance of the subjects class. The inner class initializes two instance variables sub1 and sub2.


class student:
   def __init__(self):
      self.name = "Ashish"
      self.subs = self.subjects()
      return
   def show(self):
      print("Name:", self.name)
      self.subs.display()
   class subjects:
      def __init__(self):
         self.sub1 = "Phy"
         self.sub2 = "Che"
         return
      def display(self):
         print("Subjects:", self.sub1, self.sub2)
         
s1 = student()
s1.show()
        
Output

Name: Ashish
Subjects: Phy Che
        

You can also declare an object of the outer class independently:


sub = student().subjects().display()
        

This will list out the subjects.

Types of Inner Classes

Inner classes in Python are of two types:

  • Multiple Inner Class
  • Multilevel Inner Class

Multiple Inner Class

In multiple inner classes, a single outer class contains more than one inner class. Each inner class works independently but can interact with the members of the outer class.


class Organization:
   def __init__(self):
      self.inner1 = self.Department1()
      self.inner2 = self.Department2()
        
   def showName(self):
      print("Organization Name: Tutorials Point")

   class Department1:
      def displayDepartment1(self):
         print("In Department 1")
            
   class Department2:
      def displayDepartment2(self):
         print("In Department 2")

# instance of OuterClass
outer = Organization() 
# Calling show method
outer.showName()  
# InnerClass instance 1
inner1 = outer.inner1 
# Calling display method
inner1.displayDepartment1() 
# InnerClass instance 2
inner2 = outer.inner2 
# Calling display method
inner2.displayDepartment2()
        
Output

Organization Name: Tutorials Point
In Department 1
In Department 2
        

Multilevel Inner Class

In multilevel inner classes, an inner class contains another inner class, creating multiple levels of nested classes.


class Organization:
   def __init__(self):
      self.inner = self.Department()

   def showName(self):
      print("Organization Name: Tutorials Point")

   class Department:
      def __init__(self):
         self.innerTeam = self.Team1()

      def displayDep(self):
         print("In Department")

      class Team1:
         def displayTeam(self):
            print("Team 1 of the department")

# instance of outer class                
outer = Organization()  
# call the method of outer class
outer.showName()  

# Inner Class instance
inner = outer.inner  
inner.displayDep()  

# Access Team1 instance
innerTeam = inner.innerTeam  
# Calling display method
innerTeam.displayTeam()
        
Output

Organization Name: Tutorials Point
In Department
Team 1 of the department