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
__init__ method behavior
📖 Scenario: You are creating a simple program to represent a book in a library system. Each book has a title and an author.
🎯 Goal: Build a Python class called Book that uses the __init__ method to set the title and author when a new book is created.
📋 What You'll Learn
Create a class named Book
Use the __init__ method to set title and author attributes
Create an instance of Book with specific title and author
Print the title and author of the created book
💡 Why This Matters
🌍 Real World
Classes with <code>__init__</code> methods are used to create objects that represent real things, like books, users, or products, with their own data.
💼 Career
Understanding how to initialize objects is essential for software development jobs that involve object-oriented programming, such as building apps, games, or data models.
Progress0 / 4 steps
1
Create the Book class with __init__ method
Write a class called Book with an __init__ method that takes self, title, and author as parameters. Inside __init__, set self.title to title and self.author to author.
Python
Hint
Remember, __init__ is a special method that runs when you create a new object. Use self to store the values.
2
Create a Book instance
Create a variable called my_book and assign it a new Book object with the title 'The Great Gatsby' and author 'F. Scott Fitzgerald'.
Python
Hint
Use the class name Book followed by parentheses with the title and author inside quotes.
3
Access the title and author attributes
Use a print statement to display the title and author of my_book by accessing my_book.title and my_book.author.
Python
Hint
Use dot notation to get the attributes from the object.
4
Display the book information
Write a single print statement that shows the book information in this format: Title: The Great Gatsby, Author: F. Scott Fitzgerald using an f-string and the attributes my_book.title and my_book.author.
Python
Hint
Use print(f"Title: {my_book.title}, Author: {my_book.author}") to format the output.
Practice
(1/5)
1.
What is the main purpose of the __init__ method in a Python class?
easy
A. To create a new class
B. To delete an object from memory
C. To print information about the class
D. To set up initial values for the object's attributes
Solution
Step 1: Understand the role of __init__
The __init__ method runs automatically when an object is created to set up initial values.
Step 2: Compare options with this role
Only To set up initial values for the object's attributes describes setting initial attribute values, which matches __init__'s purpose.
Final Answer:
To set up initial values for the object's attributes -> Option D
Quick Check:
__init__ sets initial values = A [OK]
Hint: Remember: __init__ sets starting info for objects [OK]
Common Mistakes:
Thinking __init__ deletes objects
Confusing __init__ with printing methods
Believing __init__ creates classes
2.
Which of the following is the correct way to define an __init__ method inside a Python class?
class Car:
?
easy
A. def __init__(self, model):
B. def init(self, model):
C. def __start__(self, model):
D. def __init__(model):
Solution
Step 1: Check method name and parameters
The method must be named exactly __init__ and include self as the first parameter.
Step 2: Compare options
Only def __init__(self, model): uses the correct name and includes self properly.
Final Answer:
def __init__(self, model): -> Option A
Quick Check:
Correct __init__ syntax = D [OK]
Hint: Always include self as first parameter in __init__ [OK]
Common Mistakes:
Omitting self parameter
Misspelling __init__ method name
Using wrong method names like init or __start__
3.
What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Buddy")
print(my_dog.name)
medium
A. Buddy
B. name
C. Dog
D. Error
Solution
Step 1: Understand object creation and attribute assignment
The __init__ method sets self.name to "Buddy" when my_dog is created.
Step 2: Check the print statement
Printing my_dog.name outputs the string "Buddy" stored in the attribute.
Final Answer:
Buddy -> Option A
Quick Check:
Object attribute value = Buddy [OK]
Hint: Print object.attribute to see stored value from __init__ [OK]
Common Mistakes:
Printing class name instead of attribute
Expecting attribute name instead of value
Assuming code causes error
4.
Find the error in this class definition:
class Person:
def __init__(name):
self.name = name
p = Person("Alice")
medium
A. Wrong class name
B. Missing self parameter in __init__
C. Missing parentheses when creating object
D. No error
Solution
Step 1: Check __init__ method parameters
The __init__ method must have self as the first parameter, but it is missing here.
Step 2: Identify impact of missing self
Without self, self.name causes an error because self is undefined.
Final Answer:
Missing self parameter in __init__ -> Option B
Quick Check:
__init__ needs self first [OK]
Hint: Always put self as first __init__ parameter [OK]