Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
116: The __init__ Constructor
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
Bookclass with an__init__method. - Assign the title, author, and isbn to instance attributes using
self. - Create two different instances of the
Bookclass (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.
There are no comments for now.