The __init__ method sets up a new object when you create it. It helps give the object its starting values.
__init__ method behavior in Python
Start learning this pattern below
Jump into concepts and practice - no test required
class ClassName: def __init__(self, parameter1, parameter2): self.attribute1 = parameter1 self.attribute2 = parameter2
self means the object itself and is always the first parameter.
You can add as many parameters as you want to give different starting values.
class Dog: def __init__(self, name): self.name = name
class Car: def __init__(self, make, year): self.make = make self.year = year
class Person: def __init__(self, name, age=0): self.name = name self.age = age
This program creates a Book object with a title and author, then prints them.
class Book: def __init__(self, title, author): self.title = title self.author = author my_book = Book("The Sun", "Alice") print(f"Title: {my_book.title}") print(f"Author: {my_book.author}")
The __init__ method does not return anything; it just sets up the object.
If you don't write an __init__ method, Python makes a default one that does nothing.
You can use self to store values that belong to the object and can be used later.
The __init__ method runs automatically when you create an object.
It helps give each object its own starting information.
You use self to keep data inside the object.
Practice
What is the main purpose of the __init__ method in a Python class?
Solution
Step 1: Understand the role of
The__init____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 DQuick Check:
__init__sets initial values = A [OK]
__init__ sets starting info for objects [OK]- Thinking
__init__deletes objects - Confusing
__init__with printing methods - Believing
__init__creates classes
Which of the following is the correct way to define an __init__ method inside a Python class?
class Car:
?Solution
Step 1: Check method name and parameters
The method must be named exactly__init__and includeselfas the first parameter.Step 2: Compare options
Only def __init__(self, model): uses the correct name and includesselfproperly.Final Answer:
def __init__(self, model): -> Option AQuick Check:
Correct__init__syntax = D [OK]
self as first parameter in __init__ [OK]- Omitting
selfparameter - Misspelling
__init__method name - Using wrong method names like
initor__start__
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)Solution
Step 1: Understand object creation and attribute assignment
The__init__method setsself.nameto "Buddy" whenmy_dogis created.Step 2: Check the print statement
Printingmy_dog.nameoutputs the string "Buddy" stored in the attribute.Final Answer:
Buddy -> Option AQuick Check:
Object attribute value = Buddy [OK]
__init__ [OK]- Printing class name instead of attribute
- Expecting attribute name instead of value
- Assuming code causes error
Find the error in this class definition:
class Person:
def __init__(name):
self.name = name
p = Person("Alice")Solution
Step 1: Check
The__init__method parameters__init__method must haveselfas the first parameter, but it is missing here.Step 2: Identify impact of missing
Withoutselfself,self.namecauses an error becauseselfis undefined.Final Answer:
Missingselfparameter in__init__-> Option BQuick Check:
__init__needsselffirst [OK]
self as first __init__ parameter [OK]- Forgetting
selfin method parameters - Thinking class name must change
- Missing parentheses when creating object
Consider this class:
class Book:
def __init__(self, title, author="Unknown"):
self.title = title
self.author = author
b1 = Book("Python 101")
b2 = Book("Learn AI", "Dr. Smith")
print(b1.author, b2.author)What will be printed?
Solution
Step 1: Understand default parameter in
The__init__authorparameter has a default value "Unknown", so it is optional when creating an object.Step 2: Analyze object creations
b1is created with only title, soauthoris "Unknown".b2provides both title and author "Dr. Smith".Step 3: Check print output
Printingb1.authorandb2.authorprints "Unknown Dr. Smith".Final Answer:
Unknown Dr. Smith -> Option CQuick Check:
Default parameters fill missing values = A [OK]
__init__ [OK]- Expecting error when argument is missing
- Mixing up title and author values
- Assuming both arguments are always required
