bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Python/Object-Oriented Python
Python•Object-Oriented Python

Python Inner Classes

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Python Inner Classes?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ Outer:
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Accessing Outer Class from Inner Class
Accessing Inner Class from the Outside
Python Inner Classes

An inner class is a class defined inside another class. The inner class can access the properties and methods of the outer class.

Inner classes are useful for grouping classes that are only used in one place, making your code more organized.

Example

class Outer:
  def __init__(self):
    self.name = "Outer Class"
class Inner:
  def __init__(self):
    self.name = "Inner Class"
  def display(self):
    print("This is the inner class")
    outer = Outer()
    print(outer.name)

Accessing Inner Class from the Outside

To access the inner class, create an object of the outer class, and then create an object of the inner class:

Example

class Outer:
  def __init__(self):
    self.name = "Outer"
class Inner:
  def __init__(self):
    self.name = "Inner"
  def display(self):
    print("Hello from inner class")
    outer = Outer()
    inner = outer.Inner()
    inner.display()

Accessing Outer Class from Inner Class

Inner classes in Python do not automatically have access to the outer class instance.

If you want the inner class to access the outer class, you need to pass the outer class instance as a parameter:

Example

class Outer:
  def __init__(self):
    self.name = "Emil"
class Inner:
  def __init__(self, outer):
    self.outer = outer
  def display(self):
    print(f"Outer class name: {self.outer.name}")
    outer = Outer()
    inner = outer.Inner(outer)
    inner.display()

Practical Example

Inner classes are useful for creating helper classes that are only used within the context of the outer class:

Example

class Car:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model
    self.engine = self.Engine()
class Engine:
  def __init__(self):
    self.status = "Off"
  def start(self):
    self.status = "Running"
    print("Engine started")
  def stop(self):
    self.status = "Off"
    print("Engine stopped")
  def drive(self):
    if self.engine.status == "Running":
      print(f"Driving the {self.brand} {self.model}")
    else:
      print("Start the engine first!")
      car = Car("Toyota", "Corolla")
      car.drive()
      car.engine.start()
      car.drive()

Multiple Inner Classes

A class can have multiple inner classes:

Example

class Computer:
  def __init__(self):
    self.cpu = self.CPU()
    self.ram = self.RAM()
class CPU:
  def process(self):
    print("Processing data...")
class RAM:
  def store(self):
    print("Storing data...")
    computer = Computer()
    computer.cpu.process()
    computer.ram.store()

Previous

Python Encapsulation

Next chapter

Files, Modules, and the Standard Library

Start with Python File Open