Explain the Python Inner Classes
Ask by Anubhav Sharma
Updated 28 Oct 2025
Let’s break it down with examples and explanations:
1. Basic Syntax
Usage:
Output:
2. Why Use Inner Classes
Inner classes are used when:
3. Outer and Inner Relationship
Inner classes can access outer class members via an instance reference.
Usage:
Output:
Here,
Addressis an inner class — it logically belongs only toPerson.4. Accessing Inner Class from Outside
You can create an instance of the inner class using the outer class name:
5. Real-World Example: Data Encapsulation
Usage:
Output:
6. Summary
Outer.Inner