Last modified: Dec 05, 2026
Does Python Have Encapsulation?
Short answer: yes, but not the way Java or C++ do it. Python doesn't have true private keywords. Instead, it relies on naming conventions and a bit of compiler-level trickery called name mangling.
That surprises a lot of people coming from stricter languages. You'll write a class, add a double underscore, and expect the attribute to be locked down. It isn't. Not really.
Still, encapsulation in Python is real and useful. You just have to understand what the language actually enforces versus what it merely suggests. Let's break it down.
What Encapsulation Actually Means
Encapsulation is about bundling data and the methods that touch that data into one unit, then controlling outside access. The goal is simple: don't let other code poke at your internals and break things.
Python supports this idea fully. Classes hold state. Methods operate on that state. You can hide implementation details behind a clean public interface.
What Python skips is forced hiding. There's no compiler error when you reach into an object from outside. That's a design choice, not an oversight.
If you want the broader picture of how this fits into object-oriented design, the guide on encapsulation in Python OOP covers the theory well.
Three Levels of Access in Python
Python gives you three conventions, each with different behavior. They're easy to remember once you see them side by side.
- Public:
self.name— accessible anywhere, no restrictions. - Protected:
self._name— a single underscore signals "internal use, please don't touch." - Private:
self.__name— double underscore triggers name mangling.
The single underscore is pure convention. Python doesn't do anything special with it. Your teammates see it and (hopefully) respect it.
The double underscore is different. The interpreter rewrites the attribute name at compile time, which makes accidental access harder. Not impossible, just harder.
How Name Mangling Works
When you write self.__balance inside a class called Account, Python stores it as _Account__balance. That's the mangling step.
Here's a quick example:
class Account:
def __init__(self, balance):
self.__balance = balance # becomes _Account__balance
def get_balance(self):
return self.__balance
acc = Account(500)
print(acc.get_balance())
print(acc.__balance) # AttributeError
500
AttributeError: 'Account' object has no attribute '__balance'
That error looks like real privacy. But it's just a renamed attribute. You can still reach it with acc._Account__balance and get 500.
So mangling prevents accidents, not attacks. That's the honest framing. And it's usually enough.
Properties: The Pythonic Way to Control Access
Here's where Python actually shines. Instead of getters and setters everywhere, you use the @property decorator. It lets you run logic when someone reads or writes an attribute.
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def celsius(self):
return self._celsius
@celsius.setter
def celsius(self, value):
if value < -273.15:
raise ValueError("Below absolute zero")
self._celsius = value
t = Temperature(25)
print(t.celsius) # 25
t.celsius = -300 # raises ValueError
25
ValueError: Below absolute zero
Notice the validation lives inside the setter. Callers still write t.celsius = 25 as if it were a plain attribute. No set_celsius() method clutter.
This is the pattern you should reach for most of the time. It gives you real control while keeping the interface clean. The details on encapsulation in Python classes go deeper into properties and class-level design.
Why Python Doesn't Force Privacy
Guido van Rossum has been clear about this: Python trusts programmers. The language assumes you're an adult who reads the docs and respects conventions.
Forced privacy also gets in the way. Testing, debugging, and quick introspection all become harder when the language fights you. Python prefers "we're all consenting adults here."
Is that the right call? Mostly yes. In practice, mangled names rarely cause problems, and the flexibility pays off during development.
Encapsulation Beyond Single Classes
Encapsulation doesn't stop at one class. It shapes how classes relate through inheritance, and that's where things get interesting.
Name mangling interacts with inheritance in subtle ways. A subclass can't easily override a double-underscore method because the names differ after mangling. That's actually intentional — it protects the parent's internals.
If you're building class hierarchies, the material on Python inheritance and polymorphism shows how encapsulated members behave across parent and child classes. It pairs well with what you've read here.
Protected members with a single underscore, on the other hand, are commonly overridden in subclasses. That's expected. The underscore is a signal, and subclasses are part of the family.
Practical Guidelines
Here's what I'd tell a junior developer on day one:
- Start with plain public attributes. Don't over-engineer.
- Add a single underscore when something is internal to your class.
- Reach for
@propertywhen you need validation or computed values. - Use double underscores sparingly — mainly to avoid name clashes in subclasses.
And don't write getters and setters for everything. That's Java habits leaking into Python. If an attribute needs no logic, expose it directly.
One more thing: document your conventions. A short docstring saying "internal, don't touch" saves more headaches than any underscore ever will.
Conclusion
Python does have encapsulation. It's just built on conventions and properties rather than access modifiers. Double underscores trigger name mangling, single underscores signal intent, and @property gives you real control when you need it.
That model works well once you stop expecting the language to enforce everything. Write clean interfaces, respect the underscores, and use properties for validation. Your future self will thank you.