---
title: "Explain the Python Inner Classes"  
description: "Explain the Python Inner Classes"  
author: "Anubhav Sharma"  
published: 2025-10-28  
updated: 2025-10-28  
canonical: https://www.mindstick.com/interview/34397/explain-the-python-inner-classes  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 5 minutes  

---

# Explain the Python Inner Classes

> In **Python**, an **inner class** (also called a **nested class**) is a class defined **inside another class**. Inner classes are often used when you want to logically group classes that are only used within the context of their enclosing class — similar to **namespacing** or **encapsulation**.

Let’s break it down with examples and explanations:

### 1. Basic Syntax

```python
class Outer:
    class Inner:
        def display(self):
            print("This is the Inner class")

    def show(self):
        print("This is the Outer class")
```

## Usage:

```python
obj_outer = Outer()
obj_outer.show()

# Access inner class through outer class
obj_inner = Outer.Inner()
obj_inner.display()
```

## Output:

```plaintext
This is the Outer class
This is the Inner class
```

### 2. Why Use Inner Classes

Inner classes are used when:

- You want to **encapsulate** helper classes that are not meant to be used outside the enclosing class.
- You want to **group related functionality** together.
- You want to **improve code organization** and readability.

### 3. Outer and Inner Relationship

Inner classes can **access outer class members** via an instance reference.

```python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.address = self.Address("Pune", "Maharashtra")

    def show(self):
        print(f"Name: {self.name}, Age: {self.age}")
        self.address.display()

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

        def display(self):
            print(f"City: {self.city}, State: {self.state}")
```

## Usage:

```python
p1 = Person("Anna", 25)
p1.show()
```

## Output:

```plaintext
Name: Anna, Age: 25
City: Pune, State: Maharashtra
```

Here, `Address` is an inner class — it logically belongs only to `Person`.

### 4. Accessing Inner Class from Outside

You can create an instance of the inner class using the **outer class name**:

```python
addr = Person.Address("Mumbai", "Maharashtra")
addr.display()
```

### 5. Real-World Example: Data Encapsulation

```python
class Computer:
    def __init__(self):
        self.cpu = self.CPU()
        self.memory = self.Memory()

    def show_specs(self):
        print("Computer Specifications:")
        self.cpu.display()
        self.memory.display()

    class CPU:
        def display(self):
            print("Processor: Intel i7")

    class Memory:
        def display(self):
            print("RAM: 16GB")
```

## Usage:

```python
comp = Computer()
comp.show_specs()
```

## Output:

```plaintext
Computer Specifications:
Processor: Intel i7
RAM: 16GB
```

### 6. Summary

| Feature | Description |
| --- | --- |
| **Definition** | Class defined inside another class |
| **Purpose** | Encapsulation, grouping, logical structure |
| **Access Syntax** | `Outer.Inner` |
| **Can Access Outer Members?** | Only via outer instance if explicitly passed |
| **Use Case** | Helper classes used only by the outer class |

## Answers

### Answer by Anubhav Sharma

> In **Python**, an **inner class** (also called a **nested class**) is a class defined **inside another class**. Inner classes are often used when you want to logically group classes that are only used within the context of their enclosing class — similar to **namespacing** or **encapsulation**.

Let’s break it down with examples and explanations:

### 1. Basic Syntax

```python
class Outer:
    class Inner:
        def display(self):
            print("This is the Inner class")

    def show(self):
        print("This is the Outer class")
```

## Usage:

```python
obj_outer = Outer()
obj_outer.show()

# Access inner class through outer class
obj_inner = Outer.Inner()
obj_inner.display()
```

## Output:

```plaintext
This is the Outer class
This is the Inner class
```

### 2. Why Use Inner Classes

Inner classes are used when:

- You want to **encapsulate** helper classes that are not meant to be used outside the enclosing class.
- You want to **group related functionality** together.
- You want to **improve code organization** and readability.

### 3. Outer and Inner Relationship

Inner classes can **access outer class members** via an instance reference.

```python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.address = self.Address("Pune", "Maharashtra")

    def show(self):
        print(f"Name: {self.name}, Age: {self.age}")
        self.address.display()

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

        def display(self):
            print(f"City: {self.city}, State: {self.state}")
```

## Usage:

```python
p1 = Person("Anna", 25)
p1.show()
```

## Output:

```plaintext
Name: Anna, Age: 25
City: Pune, State: Maharashtra
```

Here, `Address` is an inner class — it logically belongs only to `Person`.

### 4. Accessing Inner Class from Outside

You can create an instance of the inner class using the **outer class name**:

```python
addr = Person.Address("Mumbai", "Maharashtra")
addr.display()
```

### 5. Real-World Example: Data Encapsulation

```python
class Computer:
    def __init__(self):
        self.cpu = self.CPU()
        self.memory = self.Memory()

    def show_specs(self):
        print("Computer Specifications:")
        self.cpu.display()
        self.memory.display()

    class CPU:
        def display(self):
            print("Processor: Intel i7")

    class Memory:
        def display(self):
            print("RAM: 16GB")
```

## Usage:

```python
comp = Computer()
comp.show_specs()
```

## Output:

```plaintext
Computer Specifications:
Processor: Intel i7
RAM: 16GB
```

### 6. Summary

| Feature | Description |
| --- | --- |
| **Definition** | Class defined inside another class |
| **Purpose** | Encapsulation, grouping, logical structure |
| **Access Syntax** | `Outer.Inner` |
| **Can Access Outer Members?** | Only via outer instance if explicitly passed |
| **Use Case** | Helper classes used only by the outer class |


---

Original Source: https://www.mindstick.com/interview/34397/explain-the-python-inner-classes

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
