0
0
Pythonprogramming~10 mins

Instance attributes in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Instance attributes
Create object from class
Call __init__ method
Set instance attributes
Object ready with own data
Access or modify attributes
Use attributes in methods or outside
When you create an object, Python runs __init__ to set up instance attributes that hold data unique to that object.
Execution Sample
Python
class Dog:
    def __init__(self, name):
        self.name = name

d = Dog('Buddy')
print(d.name)
This code creates a Dog object with a name attribute and prints it.
Execution Table
StepActionEvaluationResult
1Create Dog object d with name 'Buddy'Calls Dog.__init__(d, 'Buddy')d.name set to 'Buddy'
2Access d.nameLook up attribute 'name' on d'Buddy'
3Print d.nameOutput the string stored in d.nameBuddy
💡 Program ends after printing the instance attribute value
Variable Tracker
VariableStartAfter Step 1After Step 2Final
dundefinedDog object with name='Buddy'Dog object with name='Buddy'Dog object with name='Buddy'
d.nameundefined'Buddy''Buddy''Buddy'
Key Moments - 2 Insights
Why do we use self.name = name inside __init__?
self.name = name sets the instance attribute 'name' for that specific object. See execution_table step 1 where d.name is set to 'Buddy'. Without self, the name would be just a local variable.
Can different objects have different values for the same attribute?
Yes! Each object has its own instance attributes. If you create another Dog with a different name, it will have its own separate name. This is shown by how d.name holds 'Buddy' only for that object.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 1, what does self.name get set to?
AThe class Dog
B'self'
C'Buddy'
DUndefined
💡 Hint
Check the 'Result' column in step 1 where d.name is set to 'Buddy'
At which step is the instance attribute 'name' accessed to print?
AStep 2
BStep 3
CStep 1
DNone
💡 Hint
Look at the 'Action' column in step 3 where d.name is printed
If we create another Dog object with name 'Max', what will d.name be?
A'Buddy'
B'Max'
CUndefined
DError
💡 Hint
Instance attributes belong to each object separately as shown in variable_tracker for d.name
Concept Snapshot
Instance attributes are variables tied to an object.
They are set inside __init__ using self.attribute = value.
Each object has its own copy of these attributes.
Access them with object.attribute syntax.
They store data unique to each object.
Full Transcript
When you create an object from a class in Python, the __init__ method runs automatically. Inside __init__, you use self.attribute = value to create instance attributes. These attributes belong to that specific object only. For example, when we create a Dog object with name 'Buddy', the attribute name is set to 'Buddy' for that object. You can access this attribute later using object.attribute, like d.name. Different objects can have different values for the same attribute name. This lets each object keep its own data separate.