MediumExperience level: 3-5 years · 8-15 years1 min read

How does encapsulation work in Python (_ , __ , and @property)?

Reviewed by Gurusankar M.

Asked inAmazonDeloitteMicrosoftInfosys
#encapsulation#private#name mangling#property#oop
Report issue

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.

On this page

Concept Made Simple

Coffee Chat Question

“How does encapsulation work in Python (_ , __ , and @property)?”

Remember It Faster

Mind Map Answer

_name
convention: internal use
__name
name-mangled to _Class__name
@property
getter/setter with validation
Philosophy
'we're all adults here'

Learn by Doing

Hands-on Keyboard

python
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)
Output
150

Think Beyond the Expected

What If?

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:

  • no true private in Python
  • _ convention, __ name mangling
  • @property for controlled access
  • validation in setters
  • public API stability

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

  1. When would you use a computed @property?
  2. How does name mangling actually rewrite the name?
  3. What are __slots__ and how do they relate?

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.