Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Purpose of constructors
📖 Scenario: Imagine you are creating a simple program to keep track of books in a library. Each book has a title and an author. You want to create a way to make new book entries easily and correctly.
🎯 Goal: Build a Python class called Book that uses a constructor to set the title and author when a new book is created. Then, print the details of the book.
📋 What You'll Learn
Create a class named Book
Add a constructor method __init__ that takes self, title, and author as parameters
Inside the constructor, set self.title and self.author to the given values
Create an instance of Book with title 'The Great Gatsby' and author 'F. Scott Fitzgerald'
Print the book's title and author in the format: Title: The Great Gatsby, Author: F. Scott Fitzgerald
💡 Why This Matters
🌍 Real World
Constructors help create objects with the right starting information, like making a new book record with its title and author.
💼 Career
Understanding constructors is essential for software development jobs that use object-oriented programming to organize and manage data.
Progress0 / 4 steps
1
Create the Book class
Create a class called Book with no content inside yet.
Python
Hint
Use the class keyword followed by the class name and a colon.
2
Add the constructor method
Inside the Book class, add a constructor method named __init__ that takes self, title, and author as parameters. Inside it, set self.title = title and self.author = author.
Python
Hint
The constructor method is named __init__ and sets the instance variables.
3
Create a Book instance
Create a variable called my_book and assign it a new Book object with title 'The Great Gatsby' and author 'F. Scott Fitzgerald'.
Python
Hint
Use the class name followed by parentheses with the title and author as arguments.
4
Print the book details
Print the book's title and author from my_book in this exact format: Title: The Great Gatsby, Author: F. Scott Fitzgerald.
Python
Hint
Use an f-string to format the output with my_book.title and my_book.author.
Practice
(1/5)
1. What is the main purpose of a constructor in a Python class?
easy
A. To print information about the class
B. To delete objects when they are no longer needed
C. To initialize new objects with starting values
D. To create new functions inside the class
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 C
Quick 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
A. def __init__(self):
B. def constructor(self):
C. def init(self):
D. def __start__(self):
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 A
Quick 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
A. Buddy says Woof!
B. Woof!
C. my_dog says Woof!
D. Error: missing argument
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 A
Quick 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
A. The print statement should be print(model)
B. The constructor name is incorrect
C. The class is missing a return statement
D. The constructor does not assign model to self.model
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 D
Quick 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
A. def __init__(self, author):
self.title = title
self.author = author
B. def __init__(self, title, author):
self.title = title
self.author = author
C. def __init__(self, title):
self.title = title
self.author = author
D. def __init__(self):
title = ''
author = ''
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 B
Quick 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]