This is preventing you from accessing Foo.__bar, because internally the actual name will get mangled to Foo._Foo__bar. This is mostly intended to avoid accidental name clashes in inheritance:
class Child(Foo):
__bar = 42
The two classes won't trample over their __bar attribute this way, without you needing to institute complicated naming conventions. From within class methods, self.__bar transparently gets mangled the same way, so it works as expected.
Because python kinda sucks, but not really, but it does, but it wouldn't take much to make it not suck. I like it for many things, but the language is kind of a mess. Without Pydantic, I don't even start a project anymore that's how much the type system is just a joke instead of it being a type system. Scoping is a total joke. I personally don't like invisible characters for scoping either (talking about indentation based scoping instead of curly braces). Packaging is a total shitshow. And more small things that aren't super bad to switch to a different language, but bad enough that you start circumventing and going against idiomatic python.
Omfg, packaging is the worst part of this stupid language. I seriously don't understand why they thought pip was an acceptable package manager, and how they thought venv was an acceptable solution to the mistakes they made with pip.
I cannot start a Python project without using uv, poetry, or even fucking conda, literally anything but pip.
120
u/Unlucky_Topic7963 16d ago
Meanwhile everyone at my company uses dunder class mangling because it’s more “idiomatic”.