0
0
Pythonprogramming~10 mins

Purpose of encapsulation in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Purpose of encapsulation
Define class with private data
Create object of class
Access data only via methods
Data is protected from outside changes
Safe and controlled data handling
Encapsulation means hiding data inside a class and only allowing access through methods to keep data safe and controlled.
Execution Sample
Python
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance
    def deposit(self, amount):
        self.__balance += amount
    def get_balance(self):
        return self.__balance
This code creates a bank account class that hides the balance and allows changes only through deposit and get_balance methods.
Execution Table
StepActionVariable/MethodValue/ResultExplanation
1Create objectaccount = BankAccount(100)account.__balance is hiddenBalance is set to 100 but hidden with __ prefix
2Call depositaccount.deposit(50)balance updated to 150Deposit method adds 50 to hidden balance
3Call get_balanceaccount.get_balance()returns 150Access balance safely via method
4Try direct accessaccount.__balanceAttributeErrorDirect access to __balance is blocked
5Try name mangling accessaccount._BankAccount__balance150Private variable can be accessed but should not be
6End--Encapsulation protects data from accidental changes
💡 Encapsulation stops direct access to private data, forcing use of methods for safety.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
__balanceN/A100 (hidden)150 (hidden)150 (hidden)150 (hidden)
Key Moments - 3 Insights
Why can't we access __balance directly from outside the class?
Because __balance is a private variable (see step 4 in execution_table), Python blocks direct access to protect data.
How do we safely change the balance?
We use the deposit method (step 2) which updates the balance inside the class, ensuring controlled changes.
What happens if we try to access the private variable using name mangling?
It works (step 5), but it breaks encapsulation rules and should be avoided to keep data safe.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the balance after calling deposit(50)?
A150
B50
C100
DAttributeError
💡 Hint
Check step 2 in the execution_table where deposit updates the balance.
At which step does direct access to __balance cause an error?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look at step 4 in the execution_table where direct access is blocked.
If we remove the __ prefix from balance, what changes in the execution?
ADeposit method will stop working
Bget_balance will return error
CDirect access to balance will be allowed
DBalance will become read-only
💡 Hint
Removing __ means the variable is no longer private, so direct access is possible.
Concept Snapshot
Encapsulation hides data inside a class using private variables (like __balance).
Access and changes happen only through methods.
This protects data from accidental or unauthorized changes.
Use methods to safely interact with hidden data.
Direct access to private data causes errors or breaks rules.
Full Transcript
Encapsulation is a way to keep data safe inside a class by hiding it with private variables. In the example, the BankAccount class hides the balance using __balance. You can only change or see the balance by calling methods like deposit or get_balance. Trying to access __balance directly causes an error, which protects the data. This helps keep the data safe and controlled. Sometimes, you can access it using special tricks, but that breaks the safety rules. Always use methods to work with private data.