0
0
Pythonprogramming~10 mins

Getter and setter methods in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Getter and setter methods
Create object with private attribute
Call getter method
Return attribute value
Call setter method
Update attribute value
Call getter method again
Return updated value
This flow shows how an object with private data uses getter to read and setter to update the value safely.
Execution Sample
Python
class Person:
    def __init__(self, name):
        self._name = name
    def get_name(self):
        return self._name
    def set_name(self, new_name):
        self._name = new_name

p = Person('Alice')
print(p.get_name())
p.set_name('Bob')
print(p.get_name())
This code creates a Person with a private name, reads it, changes it, then reads the new name.
Execution Table
StepActionVariable/MethodValue/ResultOutput
1Create Person objectp._name'Alice'
2Call get_name()p.get_name()'Alice'Alice
3Call set_name('Bob')p._name'Bob'
4Call get_name() againp.get_name()'Bob'Bob
5End of programExecution stops
💡 Program ends after printing updated name 'Bob'
Variable Tracker
VariableStartAfter Step 1After Step 3Final
p._nameundefined'Alice''Bob''Bob'
Key Moments - 3 Insights
Why do we use get_name() instead of accessing p._name directly?
Using get_name() allows controlled access to the private variable _name, as shown in step 2 of the execution_table, which helps protect the data.
What happens if we call set_name() with a new value?
As seen in step 3, set_name() updates the private variable _name safely, changing it from 'Alice' to 'Bob'.
Why is the variable named _name with an underscore?
The underscore signals that _name is meant to be private and should not be accessed directly, encouraging use of getter and setter methods.
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/Method' and 'Value/Result' columns at step 3 in the execution_table.
At which step does the program print 'Alice'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table for when 'Alice' is printed.
If we remove the set_name method call, what will be printed at step 4?
A'Bob'
B'Alice'
CError
DNone
💡 Hint
Refer to variable_tracker to see if p._name changes without set_name call.
Concept Snapshot
Getter and setter methods control access to private data.
Use get_name() to read and set_name() to update.
Private variables use underscore prefix (_name).
This protects data and allows validation.
Call getters and setters instead of direct access.
Full Transcript
This lesson shows how getter and setter methods work in Python. We create a class Person with a private variable _name. The get_name method returns the current name, and set_name updates it. We trace the program creating a Person named Alice, printing the name, changing it to Bob, and printing again. The execution table shows each step and variable changes. Key moments explain why we use getters and setters and the meaning of the underscore. The quiz asks about variable values and outputs at different steps. The snapshot summarizes how getters and setters protect and manage private data.