Featured Article

Define a Class with Class in Python Tutorial and Examples

Nicole Jul 13, 2026

Defining a class within another class in Python is a feature known as inner classes or nested classes. This concept allows you to declare a class inside another class, creating a tight association between the two. Inner classes are often used to encapsulate behavior within a class or to represent scenarios where an inner class is only meaningful in the context of its containing class.

Python Class and Objects With Code Examples - EasyCodeBook.com
Python Class and Objects With Code Examples - EasyCodeBook.com

The syntax for defining a class within another class in Python is straightforward. The inner class is declared as any regular class, using the keyword 'class', followed by the class name and the colon character inside the containing class.

a screenshot of a computer screen with the text 2 factory method in front of it
a screenshot of a computer screen with the text 2 factory method in front of it

Creating Inner Classes in Python

Here's a basic example demonstrating the creation of an inner class in Python:

What Are User-Defined Classes In Python?
What Are User-Defined Classes In Python?

class OuterClass: # The outer class def __init__(self, outer_attr): self.outer_attr = outer_attr class InnerClass: # The inner class def __init__(self, inner_attr): self.inner_attr = inner_attr

Accessing Inner Class Attributes

a screenshot of a class with the text, 1 basics of class method
a screenshot of a class with the text, 1 basics of class method

To access an attribute from the outer class within the inner class, you can use the outer class instance to reference the attribute:

outer_instance = OuterClass('Outer Attribute') inner_instance = outer_instance.InnerClass('Inner Attribute') print(inner_instance.inner_attr, outer_instance.outer_attr)

This prints: Inner Attribute Outer Attribute

a screenshot of a computer screen with the text creating a simple class in python
a screenshot of a computer screen with the text creating a simple class in python

Using the 'self' Keyword in Inner Classes

In Python, 'self' is used as a reference to the instance of the class. When defining a method within an inner class, 'self' is used to access or change the Attributes or other methods of the inner class instance.

However, 'self' in an inner class always refers to the instance of the inner class itself, unlike in outer classes where 'self' refers to the instance of the outer class.

a screenshot of a computer screen with the text 4 alternatives for class construct
a screenshot of a computer screen with the text 4 alternatives for class construct

Types of Inner Classes in Python

Python supports two types of inner classes: non-local and local inner classes.

Quick Python Quiz - Defining Classes!
Quick Python Quiz - Defining Classes!
Classes and Methods in Python
Classes and Methods in Python
a screenshot of a class - level decorator's screen with the text 5 class - level decorators
a screenshot of a class - level decorator's screen with the text 5 class - level decorators
a screen shot of a web page with the text 3 modifying class - level data
a screen shot of a web page with the text 3 modifying class - level data
how to use @ classmethod in your python classes
how to use @ classmethod in your python classes
Create a New Python Class Math1 - EasyCodeBook.com
Create a New Python Class Math1 - EasyCodeBook.com
Lets dive into Python methods- way to implement and define class behaviour.
Lets dive into Python methods- way to implement and define class behaviour.
a computer screen with text that reads,'syntax of a python class '
a computer screen with text that reads,'syntax of a python class '
Classes in Python, introduction:
Classes in Python, introduction:
Important Functions In Python
Important Functions In Python
Learn Python • #11 Classes • Create and Use Classes in Python
Learn Python • #11 Classes • Create and Use Classes in Python
Python Classes and Objects Explained — OOP for Complete Beginners
Python Classes and Objects Explained — OOP for Complete Beginners
How to Define Custom Exception Classes in Python | Towards Data Science
How to Define Custom Exception Classes in Python | Towards Data Science
Python Dataclass vs. Attrs vs. Pydantic: Defining a Class
Python Dataclass vs. Attrs vs. Pydantic: Defining a Class
Python Classes Infographic, Python Programming Course Ideas, Python Programming Reasons Infographic, Python Scientific Computing Tutorial, Python Programming Language Popularity Chart, Python Language Theoretical Classes, Programming For Computations Python Book, Python Programming Educational Poster, Scientific Computing With Python Tips
Python Classes Infographic, Python Programming Course Ideas, Python Programming Reasons Infographic, Python Scientific Computing Tutorial, Python Programming Language Popularity Chart, Python Language Theoretical Classes, Programming For Computations Python Book, Python Programming Educational Poster, Scientific Computing With Python Tips
Class And Object In Python
Class And Object In Python
an image of a computer screen with some type of programming program on the bottom right corner
an image of a computer screen with some type of programming program on the bottom right corner
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
a screenshot of a web page with the text 4 method types
a screenshot of a web page with the text 4 method types
Best Practices To Follow While Creating Classes In Python
Best Practices To Follow While Creating Classes In Python

Non-local inner classes: These can access the outer class instance variables directly. They are declared using the standard class definition syntax inside the outer class.

Non-local Inner Class Example

class OuterClass: def __init__(self): self.outer_attribute = 'non-local inner class' class InnerClass: def access_outer(self): print(self.outer_attribute)

When an instance of InnerClass calls InnerClass().access_outer(), it prints 'non-local inner class'.

Local Inner Classes

These are defined within the scope of a method, loop, or other block of code. They can only access the first-level enclosing scope and their own scope.

Note: Starting from Python 3, local classes are restricted to methods in the sense that they can only access the 'nonlocal' and 'global' scopes. They cannot reference instance variables from the enclosing class or local variables.

Now that you have a good grasp on defining classes within classes in Python, you can explore more complex use-cases and take your coding skills to the next level. Happy coding!