Remember
A leading underscore signals an internal attribute by convention. Encapsulation exposes useful operations while keeping implementation details behind them.
A leading underscore signals an internal attribute by convention. Encapsulation exposes useful operations while keeping implementation details behind them.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Account:
def __init__(self):
self._balance = 0
def deposit(self, amount):
self._balance += amount
def balance(self):
return self._balance
account = Account()
account.deposit(3)
print(account.balance())Top-level scope
class AccountObject 4 · AccountAccount({'_balance': 3})A leading underscore signals an internal attribute by convention. Encapsulation exposes useful operations while keeping implementation details behind them.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Account:
def __init__(self):
self._balance = 0
def deposit(self, amount):
self._balance += amount
def balance(self):
return self._balance
account = Account()
account.deposit(3)
print(account.balance())
|
v
DISPLAYED RESULT
3
FINAL BINDINGS
Account --> object 3 (type)
account --> object 4 (Account)
object 3: class Account
object 4: Account({'_balance': 3})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Account:
def __init__(self):
self._balance = 0
def deposit(self, amount):
self._balance += amount
def balance(self):
return self._balance
account = Account()
account.deposit(3)
print(account.balance())Top-level scope
class AccountObject 4 · AccountAccount({'_balance': 3})The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
class Account:
def __init__(self):
self._balance = 0
def deposit(self, amount):
self._balance += amount
def balance(self):
return self._balance
account = Account()
account.deposit(3)
print(account.balance())In the real world, we keep some things private or protected — like your phone password or your bank PIN. In Python, we can also keep some data inside a class private using a concept called encapsulation.
Encapsulation means hiding the details of how something works and only showing what's necessary. This helps keep things safe and organized.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person = Person("Arjun", 25)
print(person.name) # OK
print(person.age) # OK
person.age = -10 # ❌ Invalid age, but Python allows it!
print(person.age)
Arjun
25
-10
We allowed someone to set age to -10, which doesn't make sense. We need a way to protect this value.
We can make variables private by using two underscores __ in front of the variable name. This tells Python: "Don’t let anyone access this directly."
class Person:
def __init__(self, name, age):
self.name = name
self.__age = age # Private variable
def get_age(self):
return self.__age
def set_age(self, new_age):
if new_age > 0:
self.__age = new_age
else:
print("Age must be positive.")
person = Person("Arjun", 30)
print(person.name) # OK
print(person.get_age()) # OK
person.set_age(-5) # Not allowed
print(person.get_age()) # Still 30
Arjun
30
Age must be positive.
30
__age is a private variable – it cannot be accessed directly from outside.get_age() is a method to read the age.set_age() is a method to change the age — but only if it’s valid!print(person.__age)
AttributeError: 'Person' object has no attribute '__age'
This error happens because __age is private. Python doesn’t allow direct access.
Encapsulation helps keep your data safe. You hide it using private variables and provide special methods to read or change the data — this gives you full control and avoids mistakes.
Connect what you noticed in the example with the idea below.
A leading underscore signals an internal attribute by convention. Encapsulation exposes useful operations while keeping implementation details behind them.
A leading underscore makes an attribute completely inaccessible.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.