Skip to Content
Course content

116: The __init__ Constructor

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're ordering a custom gaming PC online. You don't just click a button that says "Give me a computer" and hope for the best. Instead, you fill out a configuration form: you pick the GPU, the amount of RAM, and the CPU. The company takes those specific choices and uses them to actually assemble your unique machine. When the PC arrives at your door, it isn't just a generic "computer"β€”it's your computer, with the exact specs you requested.

In Python, the __init__ method is that configuration form. It's the "constructor" that runs automatically the very moment you create a new instance of a class. Without it, your objects would all start out exactly the same, which isn't very useful in the real world.

Turning blueprints into specific objects

Let's map that PC analogy to code. The class is the blueprint for the computer, and the __init__ method is the assembly process. If we were building a system to manage smart home devices, we wouldn't want every smart light to be the same color and brightness by default. We want to decide those things at the moment we "plug them in."

class SmartLight:
    def __init__(self, room_name, color, brightness):
        self.room_name = room_name
        self.color = color
        self.brightness = brightness

# Now we "order" two different lights
kitchen_light = SmartLight("Kitchen", "Warm White", 80)
bedroom_light = SmartLight("Bedroom", "Soft Blue", 30)

Notice what happened here. I didn't call kitchen_light.__init__(). I just called SmartLight(). Python sees that call and says, "Aha, I need to run the __init__ method now to set this thing up."

Why self is non-negotiable

I'll be honest: the self keyword trips up almost everyone at first. It looks like just another argument, but it's doing something very specific. Think of self as a placeholder that says, "Whichever specific object is being created right now, put the data on that object."

When we write self.color = color, we are telling Python: "Take the color value passed into the constructor and attach it to this specific instance." That's why kitchen_light can be "Warm White" while bedroom_light is "Soft Blue," even though they both came from the same SmartLight blueprint. If we didn't use self, the data would just vanish as soon as the __init__ method finished running.

The difference between class attributes and instance attributes

You might wonder why we don't just define the variables at the top of the class. Here is the key: variables defined inside __init__ are instance attributes. They belong to that specific object.

If I put color = "White" outside of __init__, every single light in the entire house would be white, and changing one would potentially mess with all of them. By using __init__, we ensure that each object maintains its own independent state. It's the difference between a global rule for all PCs and the specific parts inside your own tower.




πŸ“‹ Practical Task

Building a Digital Library Book Tracker

You need to create a system to track books in a small library. Your task is to define a class called Book that initializes with three specific pieces of information: the title, the author, and the isbn.

Requirements:

  • Create the Book class with an __init__ method.
  • Assign the title, author, and isbn to instance attributes using self.
  • Create two different instances of the Book class (e.g., one for "The Hobbit" and one for "1984").
  • Print the title of the second book to verify that the data was stored correctly in that specific instance.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.