An inner (or nested) class is a class defined inside another class's body. It's used when a class only makes sense in the context of its enclosing class, keeping closely related concepts grouped together and out of a module's top-level namespace, rather than signaling any special relationship or automatic access between the two.
#how it works
1
Define a class inside another class’s body
Indented one level further, just like a method definition.
2
Access it through the outer class
Outer.Inner reaches the nested class by name.
3
Instantiate it like any other class
Outer.Inner() creates an instance normally.
4
No automatic access is granted
An inner class instance doesn’t automatically see its outer class’s instance data.
5
It’s purely organizational
Grouping a helper class next to the class it supports, not a distinct language feature with its own rules.
#example
class Car:
class Engine:
def start(self):
print("Engine starting...")
def __init__(self):
self.engine = Car.Engine()
my_car = Car()
my_car.engine.start()
Output
Engine starting...
1class Engine: is defined inside Car’s body, nesting it inside Car’s namespace.
2Car.Engine() creates an instance of the inner class, accessed through Car, and Car’s own __init__ stores it as self.engine.
3my_car.engine.start() calls the inner class’s method through the outer instance’s attribute, exactly like calling a method on any regular object.
#use cases
Grouping a small helper class that only makes sense in the context of its outer class
Avoiding cluttering a module's top-level namespace with classes that are implementation details of a larger one
Modeling a natural 'has-a' relationship, like a Car having an Engine, where the inner class isn't meant to be used independently
Organizing test-support classes or configuration classes closely alongside the class they belong to
Practice Python interview questions
Every Python lesson maps to the matching interview topic, with answers written for current Python versions.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses →Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free →Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books →