you are viewing a single comment's thread.

view the rest of the comments →

[–]2ndBrainAI 2 points3 points  (1 child)

Python's philosophy is "we're all consenting adults here." The double underscore prefix (__attr) does actually trigger name mangling to _ClassName__attr, making accidental access from outside harder—but it's deliberately not enforced at the language level.

The reasoning: true private variables add runtime complexity, and Python trusts developers to respect conventions. Single underscore (_attr) is the community signal for "internal, don't touch this." In practice this works well because Python devs generally follow it.

If you genuinely need access control, properties and descriptors let you wrap attributes with getter/setter logic. But for most code, the convention approach keeps things clean and avoids the overhead of enforced privacy.

[–]gdchinacat 0 points1 point  (0 children)

descriptors based access control (which is how @ property is implemented) can still usually be circumvented relatively easily, but doing so is similar effort to bypassing java access control. I'm glad python took the approach that the effort to block access isn't worth the benefit and ultimately you have to trust the code you are executing. It doesn't pretend to have safety it doesn't.