Module 4 Lesson 3: The __init__ Method
·Programming

Module 4 Lesson 3: The __init__ Method

Master the constructor. Learn how to use the special __init__ method and the 'self' keyword to initialize objects with data.

Module 4 Lesson 3: The __init__ Method

In the last lesson, we created objects and manually added attributes like dog.name = "Buddy". This is fine for one object, but if you have 10,000 objects, it’s impossible. Python solves this with a special "Constructor" method called __init__.

Lesson Overview

In this lesson, we will cover:

  • The Constructor: What __init__ does.
  • The self Keyword: Connecting data to a specific object.
  • Parameters in Classes: Passing data during creation.
  • Real-world Example: Building a more efficient User class.

1. What is __init__?

The name __init__ is short for initialize. It is a special function that runs automatically the moment you create a new object from a class. Think of it as the "birth" of the object where its basic traits are determined.


2. Meet the self Keyword

Inside a class, we use self to refer to the current object being worked on. It’s like the object saying, "Hey Python, this is my name, not some other dog’s name."

class Dog:
    def __init__(self, name, breed):
        self.name = name   # Store the input 'name' into the object's trait
        self.breed = breed # Store the input 'breed' into the object's trait

# Now we pass the data inside the parentheses!
my_dog = Dog("Buddy", "Golden Retriever")
your_dog = Dog("Max", "Husky")

print(my_dog.name)   # Output: Buddy
print(your_dog.name) # Output: Max

3. How it Works (Under the Hood)

When you run my_dog = Dog("Buddy", "Golden Retriever"):

  1. Python creates an empty object in memory.
  2. Python automatically calls __init__.
  3. The word self becomes a label for that new empty object.
  4. The data ("Buddy", "Golden Retriever") is assigned to the self labels (self.name, self.breed).
  5. The finished object is returned to the variable my_dog.

4. Default Values in __init__

Just like normal functions, you can have default values in your constructors.

class User:
    def __init__(self, username, role="Member"):
        self.username = username
        self.role = role

admin = User("MasterDev", "Admin")
guest = User("Anonymous") # role defaults to "Member"

Practice Exercise: The Smartphone Maker

Create a file named smartphones.py.

  1. Define a class Phone.
  2. Add an __init__ method that takes brand, model, and storage.
  3. Inside __init__, also create an attribute battery_level that always starts at 100 (does not need to be a parameter!).
  4. Create two phone objects with different brands and models.
  5. Print out their details and their initial battery levels.

Quick Knowledge Check

  1. What does __init__ stand for?
  2. When does the __init__ method run?
  3. In your own words, what does the self keyword represent?
  4. True or False: You have to manually call my_object.__init__() to make it work.

Key Takeaways

  • __init__ is the constructor method in Python.
  • It is used to set initial data (attributes) for your objects.
  • self is a reference to the current instance of the class.
  • Data passed during instantiation goes directly into the __init__ parameters.

What’s Next?

We know how to store data in objects. Now let's learn how to make them do something! In Lesson 4, we’ll explore Instance Methods—functions that belong specifically to an object!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn