Concept Flow - __init__ method behavior
Create object instance
Call __init__ method
Initialize attributes
Return new object
Object ready to use
When you create a new object, Python calls __init__ to set up initial values inside the object.
Jump into concepts and practice - no test required
class Dog: def __init__(self, name): self.name = name d = Dog('Buddy') print(d.name)
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Create Dog object with 'Buddy' | Calls Dog.__init__(self, 'Buddy') | New Dog object created |
| 2 | Inside __init__, assign self.name = 'Buddy' | self.name set to 'Buddy' | Object attribute name initialized |
| 3 | Return from __init__ | No return value (returns None) | Object fully initialized |
| 4 | Print d.name | Access attribute d.name | Output: Buddy |
| Variable | Start | After Step 2 | Final |
|---|---|---|---|
| self.name | undefined | 'Buddy' | 'Buddy' |
| d | undefined | Dog object created | Dog object with name='Buddy' |
__init__ is a special method called when creating an object. It sets up initial attributes using self. self.name = value stores data inside the object. __init__ does not return the object; Python does that. Use __init__ to prepare your object for use.
What is the main purpose of the __init__ method in a Python class?
__init____init__ method runs automatically when an object is created to set up initial values.__init__'s purpose.__init__ sets initial values = A [OK]__init__ sets starting info for objects [OK]__init__ deletes objects__init__ with printing methods__init__ creates classesWhich of the following is the correct way to define an __init__ method inside a Python class?
class Car:
?__init__ and include self as the first parameter.self properly.__init__ syntax = D [OK]self as first parameter in __init__ [OK]self parameter__init__ method nameinit or __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)__init__ method sets self.name to "Buddy" when my_dog is created.my_dog.name outputs the string "Buddy" stored in the attribute.__init__ [OK]Find the error in this class definition:
class Person:
def __init__(name):
self.name = name
p = Person("Alice")__init__ method parameters__init__ method must have self as the first parameter, but it is missing here.selfself, self.name causes an error because self is undefined.self parameter in __init__ -> Option B__init__ needs self first [OK]self as first __init__ parameter [OK]self in method parametersConsider 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?
__init__author parameter has a default value "Unknown", so it is optional when creating an object.b1 is created with only title, so author is "Unknown". b2 provides both title and author "Dr. Smith".b1.author and b2.author prints "Unknown Dr. Smith".__init__ [OK]