How does encapsulation work in Python (_ , __ , and @property)?
⚡ Short Answer
Python has no truly private members — it uses convention. A single underscore `_x` means 'internal, please don't touch'; a double underscore `__x` triggers name mangling (becomes _Class__x) to avoid subclass clashes. `@property` exposes controlled getters/setters so you can validate access without changing the public API.
☕Coffee Chat Question
Concept Made Simple
“How does encapsulation work in Python (_ , __ , and @property)?”
🧠Mind Map Answer
Remember It Faster
⌨️Hands-on Keyboard
Learn by Doing
class Account:
def __init__(self, balance):
self._balance = balance
@property
def balance(self):
return self._balance
@balance.setter
def balance(self, value):
if value < 0:
raise ValueError("negative balance")
self._balance = value
a = Account(100)
a.balance = 150 # goes through the setter
print(a.balance)150
🔥What If?
Think Beyond the Expected
Does a double underscore `__balance` make an attribute truly private?
No — it's name mangling, not access control. `__balance` becomes `_Account__balance`, which prevents accidental clashes in subclasses but is still reachable if you use the mangled name. Python has no enforced private; it relies on the `_` convention plus @property for controlled access.
😂Real World
@property lets you start with a plain attribute and later add validation, logging, or computed values without breaking callers — a widely used way to keep a clean public API while encapsulating internals.
🎯Interviewer's Expectation
Keywords they're listening for:
⚠️Common Mistakes
- ✗Thinking __x is enforced-private
- ✗Writing Java-style getX()/setX() instead of @property
- ✗Overusing double underscores
✅Best Practices
- ✓Use _ for internal, __ only to avoid subclass clashes
- ✓Expose validated access with @property
- ✓Keep the public attribute API stable
🔁Follow-up Questions
- 1When would you use a computed @property?
- 2How does name mangling actually rewrite the name?
- 3What are __slots__ and how do they relate?
🧩Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Encapsulation (Python) Interview question: "How does encapsulation work in Python (_ , __ , and @property)?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.