Encapsulation & Properties
Part of the Python Roadmap
In short
If any code can set balance = -50000, your bank class is broken. Encapsulation keeps internal data safe behind a clean interface, and properties let you add rules without changing how people use your class.
Encapsulation hides internal details behind a clean interface. Python marks internal attributes with a leading underscore by convention, and @property lets you add validation while keeping simple attribute syntax.

Think of it like this
The ATM and the vault
You never walk into the bank vault and grab cash. You use the ATM: it checks your PIN and balance and then hands over money. The vault (internal data) is hidden; the ATM (public methods and properties) is the safe way in. A property is an ATM that looks like a simple drawer from the outside.
Watch it happen
acc.balance = -500 with a property
1/4Code writes acc.balance = -500, as if balance were a normal attribute.
The big ideas
What you need to know
_name means internal
A single underscore says 'please don't touch this from outside'. Python won't stop you, but other developers will respect it.
__name mangling
Double underscore renames the attribute to _ClassName__name, mainly to avoid clashes in subclasses. It's not real privacy.
@property getter
Lets you read obj.balance like an attribute while a method runs behind it.
@x.setter for rules
Validate on every assignment. You can start with a plain attribute and switch to a property later without breaking callers.
See it in code
Walk through the code
1class BankAccount:2 def __init__(self, owner: str, balance: float = 0):3 self.owner = owner4 self._balance = 0.05 self.balance = balance # goes through the setter6 7 @property8 def balance(self) -> float:9 return self._balance10 11 @balance.setter12 def balance(self, value: float) -> None:13 if value < 0:14 raise ValueError("Balance can't be negative")15 self._balance = value16 17 @property18 def is_premium(self) -> bool: # read-only, computed19 return self._balance >= 10000020 21acc = BankAccount("Farhan", 5000)22acc.balance += 200023print(acc.balance) # 700024print(acc.is_premium) # False25 26try:27 acc.balance = -50028except ValueError as err:29 print(err) # Balance can't be negative1/6Store the real value in _balance, then assign through the property so even the starting balance is validated.
balance looks like a plain attribute but always passes through validation. is_premium is computed and read-only.
Save yourself the pain
Common mistakes
Oops
Writing self.balance = value inside the balance setter.
Do this instead
That calls the setter again forever (RecursionError). Store the value in self._balance.
Oops
Adding get_balance() and set_balance() methods Java-style for everything.
Do this instead
Use plain attributes first, and switch to @property only when you need validation or computed values.
Your mission
A safe Product class
Quick check
What does a leading single underscore like self._balance mean in Python?
Go deeper
- 1Store the balance as _balance and expose it through a read-only property.
- 2Add a setter that rejects negative values.
- 3Learn the difference between _single and __double underscore names.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.