Understanding Python Inner Classes
In Python, an inner class is a class that is defined within another class. It is a way to encapsulate related functionality within a parent class. Inner classes have access to the variables and methods of the outer class, allowing for a more organized and modular code structure.
Example 1: Inner Class within a Class
Let’s consider an example where we have a class called OuterClass
and within it, we define an inner class called InnerClass
:
class OuterClass:
def __init__(self):
self.outer_var = "Outer variable"
def outer_method(self):
print("This is the outer method")
class InnerClass:
def __init__(self):
self.inner_var = "Inner variable"
def inner_method(self):
print("This is the inner method")
In the above example, InnerClass
is defined within OuterClass
. It has its own variables and methods, which can be accessed using an instance of the inner class.
To use the inner class, we need to create an instance of the outer class first, and then create an instance of the inner class:
outer_obj = OuterClass()
inner_obj = outer_obj.InnerClass()
We can now access the variables and methods of the inner class using the inner_obj
instance:
print(inner_obj.inner_var)
inner_obj.inner_method()
This will output:
Inner variable
This is the inner method
Example 2: Inner Class as a Helper Class
Inner classes can also be used as helper classes within a parent class. They can provide additional functionality or perform specific tasks that are related to the parent class.
Let’s take an example where we have a class called Calculator
that performs basic arithmetic operations. Within the Calculator
class, we define an inner class called History
to keep track of the calculations performed:
class Calculator:
def __init__(self):
self.history = self.History()
def add(self, a, b):
result = a + b
self.history.add_to_history(f"Added {a} and {b} to get {result}")
return result
def subtract(self, a, b):
result = a - b
self.history.add_to_history(f"Subtracted {b} from {a} to get {result}")
return result
class History:
def __init__(self):
self.operations = []
def add_to_history(self, operation):
self.operations.append(operation)
def get_history(self):
return self.operations
In the above example, the Calculator
class has an inner class called History
. The Calculator
class uses an instance of the History
class to keep track of the operations performed.
We can use the Calculator
class as follows:
calculator = Calculator()
result = calculator.add(5, 3)
print(result)
result = calculator.subtract(10, 7)
print(result)
history = calculator.history.get_history()
print(history)
This will output:
8
3
['Added 5 and 3 to get 8', 'Subtracted 7 from 10 to get 3']
Conclusion
Python inner classes provide a way to encapsulate related functionality within a parent class. They have access to the variables and methods of the outer class, allowing for a more organized and modular code structure. Inner classes can be defined within a class or used as helper classes to provide additional functionality. They are a powerful tool for creating well-structured and maintainable code.