0
0
Pythonprogramming~10 mins

Constructor parameters in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Constructor parameters
Start object creation
Call __init__ with parameters
Assign parameters to attributes
Object ready with attributes set
Use object
When creating an object, parameters are passed to the constructor (__init__) to set up initial values.
Execution Sample
Python
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

my_dog = Dog("Buddy", 3)
print(my_dog.name, my_dog.age)
This code creates a Dog object with name and age set by constructor parameters, then prints them.
Execution Table
StepActionParameters PassedAttribute 'name'Attribute 'age'Output
1Call Dog constructorname='Buddy', age=3Not setNot set
2Inside __init__: assign self.name = namename='Buddy', age=3BuddyNot set
3Inside __init__: assign self.age = agename='Buddy', age=3Buddy3
4Constructor returns, object createdname='Buddy', age=3Buddy3
5Print my_dog.name and my_dog.ageBuddy3Buddy 3
💡 Constructor finishes, object has attributes set, program prints values.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
self.nameNot setBuddyBuddyBuddy
self.ageNot setNot set33
name (parameter)N/ABuddyBuddyN/A
age (parameter)N/A33N/A
Key Moments - 2 Insights
Why do we assign parameters to self.name and self.age inside __init__?
Because parameters are temporary values passed in; assigning them to self.name and self.age stores them as object attributes for later use, as shown in steps 2 and 3 of the execution_table.
What happens if we forget to assign a parameter to self.attribute?
The object won't have that attribute set, so trying to access it later will cause an error or show 'Not set', as seen before step 2 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of self.age after step 3?
A3
BNot set
CBuddy
DNone
💡 Hint
Check the 'Attribute age' column in row for step 3 in execution_table.
At which step does the constructor finish creating the object?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look for the row mentioning 'Constructor returns' in execution_table.
If we change the parameter name to 'Max', what will be printed at step 5?
ABuddy 3
BMax 3
CNone 3
D3 Max
💡 Hint
Refer to variable_tracker for self.name and the print output in execution_table step 5.
Concept Snapshot
Constructor parameters are inputs given when creating an object.
Inside __init__, these parameters are assigned to self attributes.
This sets up the object's initial state.
Without assignment, attributes won't exist.
Use self.attribute = parameter to store values.
Full Transcript
When you create an object in Python, you often want to give it some starting information. This is done by passing parameters to the constructor method called __init__. Inside __init__, these parameters are assigned to the object's attributes using self.attribute = parameter. This way, the object remembers these values. For example, if you create a Dog with a name and age, you pass those values to __init__, and it saves them as self.name and self.age. Later, you can use these attributes to get information about the object. If you forget to assign a parameter to self.attribute, the object won't have that attribute, and trying to use it will cause an error. The execution table shows each step: calling the constructor, assigning attributes, finishing creation, and printing the values. The variable tracker shows how self.name and self.age change from not set to their final values. This helps you understand how constructor parameters work step-by-step.