Skip to content

Inner Class in Python

diagram a nested class is just an attribute of the outer class mermaid
Nesting creates no relationship between the two beyond the name. The inner class has no reference to the outer one, no access to its attributes, and an instance of it knows nothing about any outer instance -- which is what surprises people arriving from Java. It is a namespacing tool, not a containment one.

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:

inner_class.py
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:

Command
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:

inner_class.py
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()

You can access the members of the inner class using the object of the inner class. Here is an example:

inner_class.py
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:

Command
C:\Users\username\Desktop> python inner_class.py
Inner class variable

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 decorator.
  • Inner Class with Inheritance: An inner class can inherit from another class.

A regular inner class is a class defined inside another class. Here is an example:

inner_class.py
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.

A static inner class is a class defined inside another class with the @staticmethod decorator. Here is an example:

inner_class.py
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.

An inner class can inherit from another class. Here is an example:

inner_class.py
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.

A class can have multiple inner classes. Here is an example:

inner_class.py
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.

An inner class can have another inner class inside it. Here is an example:

inner_class.py
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.

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.


sketch Nesting a class creates a namespace, and nothing else p5.js
An inner class is an attribute of the outer one. There is no implicit link back: you can build an inner instance with no outer instance in existence, and the inner methods cannot see the outer class body by bare name. That is the opposite of what nesting means in Java, which is where the expectation usually comes from.
pch.quizTag pch.quizDefaultTitle
  1. Can you create `Outer.Inner()` without an `Outer` instance?

    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.

  2. An `Inner` method refers to `SHARED`, which is defined in `Outer`'s body. What happens?

    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.

  3. Does an `Inner` instance hold a reference to an `Outer` instance?

    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.

  4. What is a nested class actually good for in Python?

    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.

Exercise 3 – Data Encapsulation with Inner Class

Section titled “Exercise 3 – Data Encapsulation with Inner Class”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading