A constructor helps create an object with initial values automatically when you make it. It saves time and keeps your code neat.
Purpose of constructors in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
class ClassName: def __init__(self, parameters): # set up initial values self.attribute = value
The __init__ method is the constructor in Python.
self refers to the object being created.
Examples
Python
class Dog: def __init__(self, name): self.name = name
Python
class Car: def __init__(self, make, year): self.make = make self.year = year
Sample Program
This program creates a Person object with a name and age using the constructor. Then it prints a greeting using those values.
Python
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name} and I am {self.age} years old.") p = Person("Alice", 30) p.greet()
Important Notes
Constructors run automatically when you create an object.
You can have parameters in constructors to set different starting values.
If you don't write a constructor, Python gives a default one that does nothing.
Summary
Constructors set up new objects with starting values.
They use the __init__ method in Python.
Using constructors keeps your code clean and easy to use.
Practice
1. What is the main purpose of a constructor in a Python class?
easy
Solution
Step 1: Understand what a constructor does
A constructor is a special method that runs when a new object is created.Step 2: Identify the purpose of initialization
It sets up the object with initial values so it is ready to use.Final Answer:
To initialize new objects with starting values -> Option CQuick Check:
Constructor = initialize objects [OK]
Hint: Constructors set starting values when creating objects [OK]
Common Mistakes:
- Confusing constructors with methods that delete objects
- Thinking constructors print information automatically
- Believing constructors create new functions
2. Which of the following is the correct way to define a constructor in a Python class?
easy
Solution
Step 1: Recall Python constructor syntax
Python uses a special method named __init__ to define constructors.Step 2: Match the exact method name
The method must be named exactly __init__ with double underscores before and after.Final Answer:
def __init__(self): -> Option AQuick Check:
Constructor method = __init__ [OK]
Hint: Constructor method is always named __init__ in Python [OK]
Common Mistakes:
- Using 'constructor' or 'init' without underscores
- Using wrong method names like __start__
- Forgetting double underscores before and after init
3. What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says Woof!"
my_dog = Dog("Buddy")
print(my_dog.bark())medium
Solution
Step 1: Understand the constructor usage
The constructor sets self.name to "Buddy" when my_dog is created.Step 2: Check the bark method output
bark returns a string with self.name followed by "says Woof!" so it returns "Buddy says Woof!".Final Answer:
Buddy says Woof! -> Option AQuick Check:
Constructor sets name, bark uses it [OK]
Hint: Constructor sets name; bark prints name with Woof [OK]
Common Mistakes:
- Ignoring the name argument in constructor
- Expecting bark to print only 'Woof!'
- Thinking my_dog is printed instead of its name
4. Identify the error in this class definition:
class Car:
def __init__(self, model):
model = model
my_car = Car("Tesla")
print(my_car.model)medium
Solution
Step 1: Check constructor assignment
The constructor assigns model to local variable model, not to self.model.Step 2: Understand attribute access
Without self.model, the object has no model attribute, causing an error on print.Final Answer:
The constructor does not assign model to self.model -> Option DQuick Check:
Use self.model = model to store attribute [OK]
Hint: Always assign to self.attribute inside __init__ [OK]
Common Mistakes:
- Assigning to local variable instead of self.attribute
- Thinking constructor name is wrong
- Expecting print(model) to work outside class
5. You want to create a class
Book that stores title and author. Which constructor correctly initializes these attributes and allows creating a Book object with both values?hard
Solution
Step 1: Check parameters needed
Both title and author must be passed to the constructor to initialize attributes.Step 2: Verify attribute assignment
Constructor must assign both self.title and self.author from parameters.Final Answer:
def __init__(self, title, author): self.title = title self.author = author -> Option BQuick Check:
Constructor with all attributes assigned = def __init__(self, title, author): self.title = title self.author = author [OK]
Hint: Constructor needs all attributes as parameters and assigns them [OK]
Common Mistakes:
- Missing parameters for all attributes
- Assigning attributes without parameters
- Using local variables instead of self attributes
