0
0
Pythonprogramming~10 mins

Adding custom attributes in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adding custom attributes
Create object
Add custom attribute
Access or modify attribute
Use attribute in code
End
This flow shows how to create an object, add a custom attribute to it, then access or modify that attribute during program execution.
Execution Sample
Python
class Car:
    pass

car = Car()
car.color = 'red'
print(car.color)
This code creates an empty class Car, adds a custom attribute 'color' to an instance, and prints its value.
Execution Table
StepActionObject StateOutput
1Define class Car (empty)Car class created, no attributes
2Create instance car = Car()car is an instance of Car, no attributes yet
3Add attribute car.color = 'red'car now has attribute 'color' with value 'red'
4Print car.colorcar.color is 'red'red
5End of programNo changes
💡 Program ends after printing the custom attribute value.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
carundefinedCar instance with no attributesCar instance with color='red'SameSame
car.colorundefinedundefined'red''red''red'
Key Moments - 2 Insights
Why can we add an attribute to an object that was not defined in the class?
In Python, objects can have attributes added dynamically after creation, as shown in step 3 of the execution_table where 'color' is added to 'car' even though Car class is empty.
What happens if we try to access an attribute that was never added?
Trying to access a non-existent attribute causes an AttributeError. Here, we only access 'car.color' after adding it in step 3, so no error occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what attribute does 'car' have?
A'color' with value 'red'
BNo attributes
C'color' with value 'blue'
D'speed' with value 100
💡 Hint
Check the 'Object State' column at step 3 in the execution_table.
At which step is the attribute 'color' first created on 'car'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for when 'car.color' appears in the variable_tracker and execution_table.
If we tried to print 'car.speed' instead of 'car.color', what would happen?
AIt would print 'speed' value if set before
BIt would print 'null'
CIt would cause an error because 'speed' was never added
DIt would print an empty string
💡 Hint
Refer to key_moments about accessing attributes not added to the object.
Concept Snapshot
Adding custom attributes in Python:
- Create an object from a class
- Add attribute by assignment: obj.attr = value
- Access attribute by obj.attr
- Attributes can be added anytime after object creation
- Accessing missing attributes causes errors
Full Transcript
This example shows how to add custom attributes to Python objects. First, we define an empty class Car. Then, we create an instance called car. Next, we add a new attribute 'color' to car by assigning car.color = 'red'. Finally, we print car.color which outputs 'red'. Python allows adding attributes dynamically to objects after they are created. Trying to access an attribute that was never added will cause an error. This is a simple way to customize objects without changing the class definition.