Nearby lessons

111 of 159

Python - Inner Classes

📌 What You Will Learn
  • Understand what an inner class is
  • Know when to use an inner class
  • Create inner class objects using different syntaxes
  • Access inner class methods through the outer class object
  • Understand the relationship between outer and inner class objects

What are Inner Classes?

Sometimes we can declare a class inside another class. Such types of classes are called inner classes.

Without the existence of one type of object, if there is no chance of the existence of another type of object, then we should go for inner classes.

Example 1: Car and Engine

Without an existing Car object, there is no chance of an existing Engine object. Hence, the Engine class should be part of the Car class.

🐍Code Cell
1class Car:
2 # car code
3 
4 class Engine:
5 # engine code
Output
No output captured.

Example 2: University and Department

Without an existing University object, there is no chance of an existing Department object.

🐍Code Cell
1class University:
2 # university code
3 
4 class Department:
5 # department code
Output
No output captured.

Example 3: Human and Head

Without an existing Human object, there is no chance of an existing Head object. Hence, Head should be part of Human.

🐍Code Cell
1class Human:
2 # human code
3 
4 class Head:
5 # head code
Output
No output captured.

Important Note

Without an existing outer class object, there is no chance of an existing inner class object.

Hence, the inner class object is always associated with the outer class object.

Demo Program-1: Basic Inner Class

🐍Code Cell
1class Outer:
2 
3 def __init__(self):
4 print("outer class object creation")
5 
6 class Inner:
7 
8 def __init__(self):
9 print("inner class object creation")
10 
11 def m1(self):
12 print("inner class method")
13 
14 
15o = Outer()
16i = o.Inner()
17i.m1()
Output
outer class object creation
inner class object creation
inner class method

Various Syntaxes for Calling Inner Class Methods

The following are the various possible syntaxes for calling the inner class method.

Syntax 1

🐍Code Cell
1o = Outer()
2i = o.Inner()
3i.m1()
Output
No output captured.

Syntax 2

🐍Code Cell
1i = Outer().Inner()
2i.m1()
Output
No output captured.

Syntax 3

🐍Code Cell
1Outer().Inner().m1()
Output
No output captured.

Demo Program-2: Inner Class to Represent Date of Birth

This program stores a Person's date of birth using an inner class.

🐍Code Cell
1class Person:
2 
3 def __init__(self):
4 self.name = 'durga'
5 self.db = self.Dob()
6 
7 def display(self):
8 print('Name:', self.name)
9 
10 class Dob:
11 
12 def __init__(self):
13 self.dd = 10
14 self.mm = 5
15 self.yy = 1947
16 
17 def display(self):
18 print('Dob={}/{}/{}'.format(self.dd, self.mm, self.yy))
19 
20 
21p = Person()
22p.display()
23 
24x = p.db
25x.display()
Output
Name: durga
Dob=10/5/1947

Explanation

  • The Person class contains an inner class Dob.
  • Inside the Person constructor, self.db = self.Dob() creates a Dob object and stores it in the instance variable db.
  • p.display() prints the name, and x.display() prints the date of birth using the Dob object.

Demo Program-3: Multiple Inner Classes

Inside a class, we can declare any number of inner classes.

🐍Code Cell
1class Human:
2 
3 def __init__(self):
4 self.name = 'Sunny'
5 self.head = self.Head()
6 self.brain = self.Brain()
7 
8 def display(self):
9 print("Hello..", self.name)
10 
11 class Head:
12 
13 def talk(self):
14 print('Talking...')
15 
16 class Brain:
17 
18 def think(self):
19 print('Thinking...')
20 
21 
22h = Human()
23h.display()
24h.head.talk()
25h.brain.think()
Output
Hello.. Sunny
Talking...
Thinking...

Explanation

  • The Human class contains two inner classes: Head and Brain.
  • The constructor creates both inner class objects and stores them in self.head and self.brain.
  • h.head.talk() and h.brain.think() call the inner class methods through the Human object.
📝 Key Takeaways
  • An inner class is a class declared inside another class
  • Use an inner class when the inner object cannot exist without the outer object
  • An inner class object is always associated with its outer class object
  • Inner class objects are created as o.Inner(), Outer().Inner(), or Outer().Inner().method()
  • A class can contain any number of inner classes

🧠 Test Your Knowledge

9 Questions
Progress: 0 / 9