r/Python • u/PalpitationOk839 • Apr 19 '26
Discussion Why doesn’t Python have true private variables like Java?
Hey everyone
Today I was learning about encapsulation in Python and honestly I got a bit surprised
In languages like Java we have proper private keywords but in Python it feels like nothing is truly private
Even with double underscores it just does name mangling and you can still access it if you really want
So I was wondering why Python is designed this way
Is it because Python follows a different philosophy or is there some deeper reason behind it
Also in real projects how do developers maintain proper encapsulation if everything can technically be accessed
Trying to understand how to think about this in a more practical and runable way
Would love to hear your thoughts 👍
106
Upvotes
5
u/ahferroin7 Apr 19 '26
Making variables private is actually more resource intensive than not doing so. It’s only very recently that any kind of mechanism for this sort of thing has made it into hardware, but all of the options for it are designed for a very different type of use case (mostly protecting memory in virtual machines from the hypervisor, or protecting a block of memory in a process from other things on the system) and would still impose some pretty nasty performance penalties. So there are performance benefits to not having enforced private scoping of things.
Python is also built more around a philosophy of developers being responsible and knowing what they’re doing, instead of assuming you need to be protected from yourself.
The standard in Python is that things prefixed with underscores are functionally not part of the public API. In practice:
A single underscore prefix is intended to not be part of the public API, but might be used internally in the module or package the class or function is defined in.
A double underscore prefix effectively signals something as the internals of the implementation, you have to jump through hoops to access it externally due to name mangling. However, it is still accessible, and this is mostly intended to cleanly ensure that a method or instance variable doesn’t get overridden by a subclass than anything else.
A double underscore prefix with a double underscore suffix is functionally a reserved name within the language itself. These are used for special purposes within Python’s data model, such as defining operator behavior as functions, providing type conversion rules, customizing class lifecycle behavior, or providing information that’s usually only of interest when debugging (for an example of the last bit, if you have an object assigned to the variable
x, you can get the name of the class of that object withx.__class__.__name__).This is all partly enforced by the data model as well actually. Wildcard imports
from foo import *ignore any class, function, or variable names in the module that are prefixed with an_, and there are a couple of other places they get explicitly ignored as well. You can also configure most linters to warn about violations in a codebase, but in practice a vast majority of it is just developers agreeing to follow the convention.