Inner Class in Python
flowchart TD A["class Outer:"] --> B["class Inner: ..."] B --> C["Inner is an ATTRIBUTE of Outer"] C --> D["reach it as Outer.Inner"] D --> E["Outer.Inner() works without an Outer instance"] E --> F["the instance has NO link to any Outer"] F --> G["no implicit access to Outer's attributes"] G --> H["pass what it needs explicitly"] C --> I["the class body of Outer is NOT a scope Inner can read"] I --> J["a name defined in Outer is not visible inside Inner's methods"]
Inner Class in Python : Definition and Usage
Section titled “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)In the above example, we have defined an inner class Inner inside the outer class Outer. The inner class 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 variableAnother 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()Accessing Inner Class Members
Section titled “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)In this example, we have created an object of the inner class Inner and accessed the 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 variableTypes of Inner Classes
Section titled “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
@staticmethoddecorator. - Inner Class with Inheritance: An inner class can inherit from another class.
Regular Inner Class
Section titled “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)In the above example, we have defined a regular inner class Inner inside the outer class Outer.
Static Inner Class
Section titled “Static Inner Class”A static inner class is a class defined inside another class with the @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)In the above example, we have defined a static inner class Inner inside the outer class Outer using the @staticmethod decorator.
Inner Class with Inheritance
Section titled “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)In the above example, we have defined an inner class Inner that inherits from the outer class Outer.
Types of Inner Classes (Categories by the level)
Section titled “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
Section titled “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)In the above example, we have defined two inner classes Inner1 and Inner2 inside the outer class Outer.
Multilevel Inner Classes
Section titled “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)In the above example, we have defined an inner class Inner2 inside the inner class Inner1.
Conclusion
Section titled “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.
Check yourself
Section titled “Check yourself”-
Can you create `Outer.Inner()` without an `Outer` instance?
Verified. Nesting creates a namespace, not containment. The inner class is an ordinary attribute and is instantiated like any other class.
pch.quizShowAnswer
B — Yes — `Inner` is just an attribute of `Outer` — Verified. Nesting creates a namespace, not containment. The inner class is an ordinary attribute and is instantiated like any other class.
-
An `Inner` method refers to `SHARED`, which is defined in `Outer`'s body. What happens?
Verified. A class body is not an enclosing scope for functions defined inside it. `Outer.SHARED` works because it is an explicit lookup.
pch.quizShowAnswer
B — `NameError: name 'SHARED' is not defined` — Verified. A class body is not an enclosing scope for functions defined inside it. `Outer.SHARED` works because it is an explicit lookup.
-
Does an `Inner` instance hold a reference to an `Outer` instance?
This is the main difference from Java's inner classes. If the inner object needs the outer one, pass it in explicitly.
pch.quizShowAnswer
B — No — there is no link at all — This is the main difference from Java's inner classes. If the inner object needs the outer one, pass it in explicitly.
-
What is a nested class actually good for in Python?
It groups a helper with its owner and keeps the module namespace tidy. It provides no access, no privacy and no lifecycle relationship.
pch.quizShowAnswer
B — Namespacing — signalling that the class belongs to the outer one — It groups a helper with its owner and keeps the module namespace tidy. It provides no access, no privacy and no lifecycle relationship.
Try it: Inner Class Exercises
Section titled “Try it: Inner Class Exercises”Exercise 1 – Define Inner Class
Section titled “Exercise 1 – Define Inner Class”Exercise 2 – Outer Accessing Inner
Section titled “Exercise 2 – Outer Accessing Inner”Exercise 3 – Data Encapsulation with Inner Class
Section titled “Exercise 3 – Data Encapsulation with Inner Class”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading