0
0
Pythonprogramming~10 mins

Property decorator usage in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Property decorator usage
Define class with private attribute
Define @property method
Access property: calls getter method
Define @property.setter method
Assign to property: calls setter method
Property value updated internally
Access property again to get updated value
The flow shows how a class uses @property to create a getter and setter for a private attribute, allowing controlled access and update.
Execution Sample
Python
class Person:
    def __init__(self, name):
        self._name = name

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

p = Person('Alice')
print(p.name)
p.name = 'Bob'
print(p.name)
This code creates a Person class with a private _name attribute and uses @property to get and set the name safely.
Execution Table
StepActionVariable/AttributeValueOutput
1Create Person instance with name 'Alice'p._name'Alice'
2Access p.name (calls getter)p.name'Alice'Alice
3Assign p.name = 'Bob' (calls setter)p._name'Bob'
4Access p.name again (calls getter)p.name'Bob'Bob
5End of programExecution stops
💡 Program ends after printing updated property value.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
p._nameundefined'Alice''Bob''Bob'
p.name (getter)undefined'Alice''Bob''Bob'
Key Moments - 3 Insights
Why do we use _name instead of name inside the class?
We use _name as a private attribute to avoid direct access. The property methods control how name is accessed or changed, as shown in steps 1 and 3 of the execution_table.
What happens when we assign p.name = 'Bob'?
The setter method is called, which updates the private _name attribute. This is shown in step 3 where p._name changes to 'Bob'.
Why does print(p.name) call a method instead of accessing a variable?
Because of the @property decorator, p.name calls the getter method, which returns the private _name value. This is shown in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p._name after step 3?
A'Bob'
B'Alice'
Cundefined
DNone
💡 Hint
Check the 'Variable/Attribute' and 'Value' columns at step 3 in execution_table.
At which step does the getter method return 'Alice'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for when p.name is accessed and output is 'Alice' in execution_table.
If we remove the @name.setter method, what happens when we assign p.name = 'Bob'?
AIt updates p._name to 'Bob' anyway
BIt silently ignores the assignment
CIt raises an AttributeError
DIt creates a new attribute p.name
💡 Hint
Think about how property setters control assignment, and what happens if setter is missing.
Concept Snapshot
Use @property to create a getter method for a private attribute.
Use @propertyname.setter to create a setter method.
Accessing the property calls the getter.
Assigning to the property calls the setter.
This controls attribute access safely and cleanly.
Full Transcript
This visual execution shows how the property decorator works in Python. We start by creating a class with a private attribute _name. The @property decorator makes a method act like a readable attribute. When we access p.name, it calls the getter method and returns _name. The @name.setter decorator allows us to assign to p.name, which calls the setter method and updates _name. The execution table traces these steps, showing how p._name changes from 'Alice' to 'Bob'. Key moments clarify why we use a private attribute and how the getter and setter methods work. The quiz tests understanding of variable values at each step and what happens if the setter is missing.