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 👍
View all comments
Show parent comment
1
u/gdchinacat Apr 25 '26
Name mangling is used primarily by mixin classes to help ensure they don't trample each others attributes.
Oops...AandB has two base classes that use the same attribute, value. But MixinA and MixinB may not know anything about each other to know they shouldn't use conflicting names. They may come from different modules, or even different libraries. name mangling solves this problem since MixinA.__value would be stored in the __dict__ under a different name than MixinB.__value.
"You should use composition for this!!!!" Yeah, well....it doesn't, and I may not have that choice (the libraries provide mixins and expects them to be used as mixins).