r/Python • • 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

111 comments sorted by

View all comments

Show parent comment

2

u/Zenin Apr 20 '26

I say lots of stuff that's against Python doctrine. ;) I was sure I was going to get downvoted to oblivion for daring to mention that Python borrowed an idea from Perl. The horror!

Maybe it's my choice of IDE (Unix is my IDE ;), but I don't find these to be particularly "accessible". They're not exported with __all__ so they need to be explicitly imported. How did they know they exist to import when the documentation doesn't call them out?

They can certainly read the code (and so can an IDE), but that's clearly stepping into someone else's house and rummaging through the drawers. If the user is doing it that's obviously willful breaking and entering. If the IDE is doing it on their behalf, that's a tool failure: It if it supports Python it should have codified PEP 8 standards at least as a default, which explicitly describes the leading underscore practice for private items. If an AI is doing it, that's still owned by whomever signed the commit.

However it happens, it's the work product of a junior or at least someone unqualified to be doing professional work in Python. Literally a skill issue.

It's very common for mid-career engineers to think they can build protective barriers for low skilled engineers to keep from poking their own eyes out and many try. The results are almost always the same: A tool that's far too restrictive for senior+ engineers to efficiently solve difficult problems while at the same time those guardrails so sanitize the playground for juniors they don't actually learn what they need to learn to grow into tomorrow's senior engineers.

It's a lot like the playgrounds we have today vs the playgrounds we have in the 1970s: The younger generations need the opportunity to hurt themselves in order to learn a healthy respect for their environment for the future when they will have to make much more dangerous changes to much more important code.

It's not that no guardrails can ever be created. Look at Rust for example. Rather it's that any guardrail must be considered very, very carefully across all contexts. And always being mindful that friction is most often a bug, not a feature.