Inner Class in Python
Inner Class in Python : Definition and Usage
In Python, an inner class is a class defined inside another class. Inner classes are used to logically group classes together. They are used to represent a relationship between the outer class and the inner class.
An inner class can access all the members of the outer class, but the reverse is not true. The outer class cannot access the members of the inner class.
Here is an example of an inner class in Python:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
outer = Outer()
inner = outer.Inner()
print(inner.inner_var)
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
outer = Outer()
inner = outer.Inner()
print(inner.inner_var)
In the above example, we have defined an inner class Inner
Inner
inside the outer class Outer
Outer
. The inner class Inner
Inner
has its own variables and methods. We can create an instance of the inner class using the outer class instance.
When we run the above code, it will output:
C:\Users\username\Desktop> python inner_class.py
Inner class variable
C:\Users\username\Desktop> python inner_class.py
Inner class variable
Another way to create an instance of the inner class is by using the outer class name. Here is an example:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner().inner_method()
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner().inner_method()
Accessing Inner Class Members
You can access the members of the inner class using the object of the inner class. Here is an example:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner()
print(inner.inner_var)
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner()
print(inner.inner_var)
In this example, we have created an object of the inner class Inner
Inner
and accessed the inner_var
inner_var
variable using the object.
When we run the above code, it will output:
C:\Users\username\Desktop> python inner_class.py
Inner class variable
C:\Users\username\Desktop> python inner_class.py
Inner class variable
Types of Inner Classes
There are three types of inner classes in Python:
- Regular Inner Class: A regular inner class is a class defined inside another class.
- Static Inner Class: A static inner class is a class defined inside another class with the
@staticmethod
@staticmethod
decorator. - Inner Class with Inheritance: An inner class can inherit from another class.
Regular Inner Class
A regular inner class is a class defined inside another class. Here is an example:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner()
print(inner.inner_var)
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner()
print(inner.inner_var)
In the above example, we have defined a regular inner class Inner
Inner
inside the outer class Outer
Outer
.
Static Inner Class
A static inner class is a class defined inside another class with the @staticmethod
@staticmethod
decorator. Here is an example:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
@staticmethod
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer.Inner()
print(inner.inner_var)
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
@staticmethod
class Inner:
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer.Inner()
print(inner.inner_var)
In the above example, we have defined a static inner class Inner
Inner
inside the outer class Outer
Outer
using the @staticmethod
@staticmethod
decorator.
Inner Class with Inheritance
An inner class can inherit from another class. Here is an example:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner(Outer):
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner()
print(inner.inner_var)
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner(Outer):
def __init__(self):
self.inner_var = "Inner class variable"
def inner_method(self):
print("Inner class method")
inner = Outer().Inner()
print(inner.inner_var)
In the above example, we have defined an inner class Inner
Inner
that inherits from the outer class Outer
Outer
.
Types of Inner Classes (Categories by the level)
- Multiple Inner Classes: A class can have multiple inner classes.
- Multilevel Inner Classes: An inner class can have another inner class inside it.
Multiple Inner Classes
A class can have multiple inner classes. Here is an example:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner1:
def __init__(self):
self.inner_var1 = "Inner class variable 1"
def inner_method1(self):
print("Inner class method 1")
class Inner2:
def __init__(self):
self.inner_var2 = "Inner class variable 2"
def inner_method2(self):
print("Inner class method 2")
inner1 = Outer().Inner1()
inner2 = Outer().Inner2()
print(inner1.inner_var1)
print(inner2.inner_var2)
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner1:
def __init__(self):
self.inner_var1 = "Inner class variable 1"
def inner_method1(self):
print("Inner class method 1")
class Inner2:
def __init__(self):
self.inner_var2 = "Inner class variable 2"
def inner_method2(self):
print("Inner class method 2")
inner1 = Outer().Inner1()
inner2 = Outer().Inner2()
print(inner1.inner_var1)
print(inner2.inner_var2)
In the above example, we have defined two inner classes Inner1
Inner1
and Inner2
Inner2
inside the outer class Outer
Outer
.
Multilevel Inner Classes
An inner class can have another inner class inside it. Here is an example:
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner1:
def __init__(self):
self.inner_var1 = "Inner class variable 1"
def inner_method1(self):
print("Inner class method 1")
class Inner2:
def __init__(self):
self.inner_var2 = "Inner class variable 2"
def inner_method2(self):
print("Inner class method 2")
inner1 = Outer().Inner1()
inner2 = inner1.Inner2()
print(inner2.inner_var2)
class Outer:
def __init__(self):
self.outer_var = "Outer class variable"
def outer_method(self):
print("Outer class method")
class Inner1:
def __init__(self):
self.inner_var1 = "Inner class variable 1"
def inner_method1(self):
print("Inner class method 1")
class Inner2:
def __init__(self):
self.inner_var2 = "Inner class variable 2"
def inner_method2(self):
print("Inner class method 2")
inner1 = Outer().Inner1()
inner2 = inner1.Inner2()
print(inner2.inner_var2)
In the above example, we have defined an inner class Inner2
Inner2
inside the inner class Inner1
Inner1
.
Conclusion
In this article, you learned about inner classes in Python. You also learned about the different types of inner classes and how to create and use them in Python. Inner classes are a powerful feature of Python that allows you to logically group classes together and represent relationships between classes. For more information, you can refer to the official documentation. For more tutorials, you can visit the Python Central Hub.
Was this page helpful?
Let us know how we did