0
0
Pythonprogramming~10 mins

Public attributes in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Public attributes
Create object
Access public attribute
Read or modify attribute value
Use updated value in program
End
This flow shows how a public attribute of an object is created, accessed, and modified directly.
Execution Sample
Python
class Car:
    def __init__(self, color):
        self.color = color

car = Car('red')
print(car.color)
car.color = 'blue'
print(car.color)
This code creates a Car object with a public attribute 'color', prints it, changes it, then prints again.
Execution Table
StepActionAttribute 'color' ValueOutput
1Create Car object with color='red'red
2Print car.colorredred
3Change car.color to 'blue'blue
4Print car.colorblueblue
5End of programblue
💡 Program ends after printing updated attribute value.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
car.colorundefinedredblueblue
Key Moments - 2 Insights
Why can we access and change car.color directly?
Because 'color' is a public attribute, it can be accessed and modified directly as shown in steps 2 and 3 of the execution_table.
What happens if we try to access car.color before creating the object?
The attribute does not exist before object creation, so accessing it would cause an error. Step 1 shows creation where 'color' is set.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of car.color after step 3?
A'red'
Bundefined
C'blue'
D'green'
💡 Hint
Check the 'Attribute color Value' column at step 3 in the execution_table.
At which step does the program print the original color 'red'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table for the first print.
If we remove the line 'car.color = "blue"', what will be printed at step 4?
A'red'
B'blue'
CError
DNothing
💡 Hint
Without changing the attribute, it keeps its original value as shown in variable_tracker.
Concept Snapshot
Public attributes in Python classes are variables accessible directly from object instances.
They can be read or changed freely using dot notation (object.attribute).
No special methods are needed to access or modify them.
Example: obj.attr = value or print(obj.attr).
They provide simple, direct data storage in objects.
Full Transcript
This lesson shows how public attributes in Python work. We create a class Car with a public attribute 'color'. When we make a Car object, we set 'color' to 'red'. We can print this value directly using car.color. Then we change car.color to 'blue' and print it again. The execution table traces each step, showing how the attribute value changes and what is printed. Public attributes are easy to use because they allow direct access and modification without extra code. Beginners often wonder why they can change attributes directly; this is because public attributes have no access restrictions. The visual quiz tests understanding of attribute values at different steps and what happens if we skip changing the attribute. Remember, public attributes are the simplest way to store and update data inside objects.