0
0
Pythonprogramming~10 mins

Protected attributes in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protected attributes
Define class with _protected attribute
Create object instance
Access _protected attribute inside class
Access _protected attribute outside class
Modify _protected attribute
Program continues
This flow shows how a protected attribute (with a single underscore) is defined and accessed inside and outside a class, highlighting that access outside is allowed but discouraged.
Execution Sample
Python
class Car:
    def __init__(self):
        self._speed = 0

car = Car()
print(car._speed)
Defines a class with a protected attribute _speed, creates an object, and prints the protected attribute.
Execution Table
StepActionVariable/AttributeValueOutput
1Define class Car with __init___speedDefined as protected attribute
2Create instance carcarCar object created
3Call __init__ for carcar._speed0
4Print car._speedcar._speed00
5End of program
💡 Program ends after printing the protected attribute value 0
Variable Tracker
VariableStartAfter Step 2After Step 3Final
carundefinedCar object createdCar object with _speed=0Car object with _speed=0
car._speedundefinedundefined00
Key Moments - 2 Insights
Why can we access the _speed attribute outside the class even though it is 'protected'?
In Python, a single underscore prefix is a convention to indicate 'protected', but it does not prevent access. As shown in step 4 of the execution_table, accessing car._speed outside the class works but is discouraged.
What happens if we try to modify the _speed attribute outside the class?
You can modify it freely because Python does not enforce protection. The underscore is just a hint to programmers. This is implied by the convention and the execution flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of car._speed when printed?
AUndefined
B0
CError
DNone
💡 Hint
Check the 'Value' and 'Output' columns at step 4 in the execution_table.
At which step is the protected attribute _speed first assigned a value?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Variable/Attribute' and 'Value' columns in the execution_table to see when car._speed is set.
If we rename _speed to speed (no underscore), how would the access outside the class change?
AIt would cause an error when accessed outside
BIt would become private and inaccessible
CIt would still be accessible without any warning
DIt would require special syntax to access
💡 Hint
Recall that underscore is a convention; removing it does not restrict access (see key_moments).
Concept Snapshot
Protected attributes in Python start with a single underscore (_).
This is a convention to signal 'internal use' but does not prevent access.
You can access and modify them outside the class, but it's discouraged.
Use them to indicate 'please don't touch this' to other programmers.
Full Transcript
This visual execution shows how protected attributes work in Python. A class Car defines a protected attribute _speed with a single underscore. When an object car is created, _speed is set to 0 inside the constructor. Accessing car._speed outside the class prints 0 without error. This is because Python's single underscore is a naming convention only, not an access restriction. The execution table traces each step: defining the class, creating the object, initializing _speed, and printing it. The variable tracker shows how car and car._speed change over time. Key moments clarify that protected attributes can be accessed and modified outside the class, but it is discouraged by convention. The quiz tests understanding of when _speed is set, its value when printed, and the effect of removing the underscore. The snapshot summarizes the concept simply for quick reference.