Introduction π
An **Inner Class** (also called a **Nested Class**) is a class defined **inside another class**. It helps logically group classes that are only relevant within the outer class, improving structure and readability.
Note
π‘ They help create modular, organized code
π‘ Syntax: class Outer: class Inner: ...
1. Basic Example of an Inner Class π§±
basic_inner_class.py
class Outer:
class Inner:
def show(self):
print("This is the Inner class")
o = Outer.Inner()
o.show()β Inner class is accessed using Outer.Inner()
β Useful for grouping closely related logic
2. Outer Class Accessing Inner Class π
outer_access_inner.py
class Outer:
def __init__(self):
self.inner = self.Inner()
class Inner:
def display(self):
print("Inner class method")
o = Outer()
o.inner.display()β Outer class can create and use objects of the inner class
3. Inner Class Accessing Outer Class Attributes π§
inner_access_outer.py
class Outer:
def __init__(self, value):
self.value = value
class Inner:
def show(self, outer_obj):
print("Outer value is:", outer_obj.value)
o = Outer(50)
inner = Outer.Inner()
inner.show(o)β Inner class receives the outer class instance as a parameter
4. Practical Example β Computer and CPU π»
computer_cpu.py
class Computer:
def __init__(self):
self.cpu = self.CPU()
self.ram = self.RAM()
def show_specs(self):
print("CPU:", self.cpu.cores, "cores")
print("RAM:", self.ram.size, "GB")
class CPU:
def __init__(self):
self.cores = 8
class RAM:
def __init__(self):
self.size = 16
comp = Computer()
comp.show_specs()β Inner classes help structure related components
β CPU & RAM belong inside Computer β good encapsulation
5. Real-World Example β University System π
university_system.py
class University:
def __init__(self, name):
self.name = name
self.department = self.Department()
def show(self):
print("University:", self.name)
self.department.show()
class Department:
def show(self):
print("Department: Computer Science")
u = University("Harvard")
u.show()6. When Should You Use Inner Classes? π―
- β When the inner class is tightly coupled with the outer class
- β When an inner class should not be used independently
- β To group related logic in a clean, readable way
- β Useful for components (e.g., Engine inside Car)
7. Nested Classes and OOP Structure π§©
| Outer Class | Inner Class |
|---|---|
| Represents main entity | Represents a part of that entity |
| Can create objects of inner class | Rarely used outside the outer class |
| Holds core logic | Holds supporting logic |
8. Inner Class Inside Method (Local Inner Class) π§ͺ
local_inner_class.py
class Outer:
def process(self):
class LocalInner:
def message(self):
print("Local Inner Class")
obj = LocalInner()
obj.message()
o = Outer()
o.process()β Inner class exists only inside a method
β Useful for encapsulating temporary or helper logic
9. Private Inner Classes π
private_inner_class.py
class Outer:
class __Inner:
def show(self):
print("Private Inner Class")
# Accessing private inner class (name-mangling)
inner = Outer._Outer__Inner()
inner.show()Note
β Rarely used but important for security-sensitive code
10. Best Practices π‘
- β Use inner classes only when they are tightly bound to outer class
- β Avoid overusing them β can make code hard to read
- β Good for modeling components (Engine inside Car, CPU inside Computer)
- β Name inner classes clearly for readability
Conclusion π
You now fully understand Inner Classes in Python! Want the next topic? Try Magic Methods, Composition, Interfaces, or Class Relationships. Just tell me! π