Python Inner Classes: A Guide to Using Class in a Class

Written by:Nathan Rosidi
Inner classes, an advanced form of Python classes, are explained with simple use cases, common mistakes, and best practices.
Python classes let you create objects. Later, you can access them easily. It makes this entire design reusable. Also, it simplifies complex code into an understandable, neat version. But there are advanced designs of classes called inner classes.
This looks complicated, but in this article, we'll simplify and explore it, starting with fundamentals, use cases, common mistakes, and best practices, and finish with alternative designs.
What Does “Class in a Class” Mean in Python?
Class in a class or inner class means there are two different classes inside each other. Let’s see the syntax.
class Tesla #class
class Model3 #class in a class
battery = "Long Range" #object and attributes
model = “Model 3"
price = “42940 “
Before learning more about inner classes, let’s discover Python classes first.
Python Classes
Python is an object-oriented programming language. But what does it mean? It means you can build programs by creating objects. These objects can hold data. And you can call them whenever you want.
And Python classes allow you to use this design. In them, you can access objects under these classes, which hold data. Here is a sample.
class Tesla3:
battery = "Long Range"
model = "Model 3"
price = 42490
class TeslaS:
battery = "Dual Motor"
model = "Model S"
price = 81980
Here, we defined two classes with three attributes each. Let’s check Tesla Model 3’s price.
print(f"Tesla 3's price is {Tesla3.price} USD")
Let’s see the output.

If you don’t want to define Tesla 3 price over and over again, and want to access them whenever you want with other attributes like battery, model, or price; you have to use Classes.
If you want to know more, read this Python class method article.
Inner Classes
Let’s see how inner classes work in code. In the following code, we’ll define a class that combines two classes we defined, which have each model's features(attributes), like Model 3 or Model S.
class Tesla:
class Model3:
battery = "Long Range"
model = "Model 3"
price = 42490
class ModelS:
battery = "Dual Motor"
model = "Model S"
price = 81980
And let’s access the data held by Model3’s price attribute. Simply put: Let’s check the Model 3 price.
print(Tesla.Model3.price, "USD")Let’s see the output.

Why and When to Use an Inner Class
Now, let’s explore why inner classes (nested classes) can be used with use cases.

Use-Cases
So far, we learned how to define classes, inner classes, and why to use them. In this section, we’ll show use cases.
1. School System
In school, each classroom has students, and each student has an ID. Let’s program this system, using inner classes.
First, we’ll create the school name under the school class. And in the first level of inner class, we’ll create a classroom, which includes room_number and grade_level.
And for each grade and room, there will be students. So we’ll create this class under Classroom, so this will be the second level of the inner class. Here is the code.
class School:
name = "Green Valley School"
class Classroom:
room_number = "A12"
grade_level = 5
class Student:
student_name = "Alice Brown"
student_id = 101
Now, let’s call the student's name and ID. Here is the code.
print(School.Classroom.Student.student_name)
print(School.Classroom.Student.student_id)
Let’s see the output.

2. Shopping App
Each shopping website or app includes price tags. In this use case, there is one inner class, which holds the information about this item’s name and price. Let’s see the code.
class Order:
order_id = 3001
class Item:
item_name = "Phone Case"
price = 12.99
Let’s access the phone case and its price.
# call the item info
print(Order.Item.item_name)
print(Order.Item.price)
Here is the output.

3. Library System
In a library, there are plenty of books, and all of these books include chapters, and each of these chapters has a different page count.
In this example, we’ll create two inner classes: Book under Library and Chapter under Book.
class Library:
library_name = "City Library"
class Book:
title = "The Silent Lake"
author = "Jane Hill"
class Chapter:
chapter_title = "Morning Light"
page_count = 14
Let’s see the chapter of this book and the page count.
# call the chapter info
print(Library.Book.Chapter.chapter_title)
print(Library.Book.Chapter.page_count)
Here is the output.

Patterns for Nested Classes
In nested classes, there are simple patterns you should know. For instance, a helper class can be kept inside since it serves only the main class. Like this, there are different patterns, which you’ll see in this table.

Common Mistakes & Best Practices
If you use an inner class when the normal class is easier, this would be over-engineering. Similar to this issue, let’s see mistakes and best practices that should be used instead of these mistakes.

Nested Classes vs Alternative Designs
There are alternative designs in Python that can be used instead of nested classes. For instance, a normal separate class we already explored. But there are more designs that can be used as an alternative to nested classes, like module packages and compositions.
Let’s see different options, when they work well, and notes about them in a table.

Conclusion
Nested classes are just an advanced version of normal classes. They help make your code easier to read and keep everything organized. Still, you have to follow best practices like only using them when actually needed.
In many cases, normal classes are enough, so make sure you do not over-engineer, but when needed, do so by following best practices and by avoiding common mistakes.
FAQs
Can I instantiate the inner class directly (without outer instance)?
Yes, you can create it directly by calling it with the outer class name.
Does a nested class inherit the outer class’s attributes automatically?
No, it does not, unless you pass them in.
Are nested classes common in Python?
No, they are not common; they are used if the regular classes are not enough.
Does using nested classes affect performance?
Not quietly, the performance effect is negligible.
How does this work with the inheritance of outer classes?
If the outer class is inherited, the nested class is also reachable.
Share


