×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Inner Class

Last Updated : May 03, 2025

In Python, a class defined inside another class is known as an inner class or nested class. Inner classes are used to group classes that are only used in one place.

In this chapter, we will learn how to define and use inner classes with the help of examples.

Defining and Accessing an Inner Class

An inner class is defined inside an outer class using regular class syntax. You can create an object of the inner class using the outer class reference.

Example

In this example, we define a College class with an inner class called Department:

# Defining the outer class
class College:
    def __init__(self, name):
        self.name = name

    def show(self):
        print(f"College Name: {self.name}")

    # Defining the inner class
    class Department:
        def __init__(self, dept_name):
            self.dept_name = dept_name

        def show(self):
            print(f"Department: {self.dept_name}")

# Creating objects
c = College("IIT Delhi")
c.show()

# Creating object of inner class
d = College.Department("Computer Science")
d.show()

The output of the above code would be:

College Name: IIT Delhi
Department: Computer Science

Accessing Inner Class from a Method of Outer Class

You can also create and use an inner class object within a method of the outer class.

Example

In this example, we define a School class with an inner class Student created and accessed through a method.

class School:
    def __init__(self, school_name):
        self.school_name = school_name

    def create_student(self, name):
        student = self.Student(name)
        student.display()

    # Inner class
    class Student:
        def __init__(self, name):
            self.name = name

        def display(self):
            print(f"Student Name: {self.name}")

# Creating outer class object
s = School("Kendriya Vidyalaya Hyderabad")
s.create_student("Anjali")

The output of the above code would be:

Student Name: Anjali

Using Inner Class Independently

An inner class can be used independently by referencing it with the outer class name.

Example

In this example, we use the inner class Address from the Employee class without creating an outer class object.

class Employee:
    class Address:
        def __init__(self, city, pincode):
            self.city = city
            self.pincode = pincode

        def display(self):
            print(f"City: {self.city}, Pincode: {self.pincode}")

# Using inner class directly
addr = Employee.Address("Bengaluru", 560001)
addr.display()

The output of the above code would be:

City: Bengaluru, Pincode: 560001

Real-World Use Case of Inner Class

For instance, a University may contain a Course class, and that course doesn't make sense outside the context of a university.

Example

Here, we define a University class with a nested Course class.

class University:
    def __init__(self, uni_name):
        self.uni_name = uni_name

    class Course:
        def __init__(self, course_name):
            self.course_name = course_name

        def display(self):
            print(f"Course: {self.course_name}")

# Creating course directly using inner class
course1 = University.Course("Data Science")
course1.display()

The output of the above code would be:

Course: Data Science

Python Inner Class Exercise

Select the correct option to complete each statement about inner classes in Python.

  1. An inner class is defined ___ another class.
  2. To create an instance of an inner class, you must first create an instance of the ___ class.
  3. Accessing the inner class requires referencing it as ___ within the outer class.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.