>>> class Foo:
def __fn(self):
return 2
>>> Foo().__fn()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__fn'
>>> Foo()._Foo__fn()
2
A single underscore is more like 'protected', ie. it doesn't prevent subclasses from easily/accidentally overriding your function.Python keeps code much smaller but above a certain size I wish I could "promote" parts of my code to Java.
Partial typing in Python just isn't close to feeling the same.
There are many benefits to enforcing privacy, and one of the biggest ones is that you can make changes to the internals of your library without breaking your users' code, because they never had access to the internals.
Don't stop me from patching your buggy code. If writing an extension is more efficient, that's how I'd solve the problem.
Of course, if you have a perfect understanding of the code, this will never be a problem, as the program is always executed as it is written. But most likely, not everyone in your organization will be as patient, smart and attentive to the details as you are.
It's a way for the class user to say "I know this is terrible, I know this might break, I want to do it anyway".
There are both declaritive and command line param ways to open things so it's not as rigid as it sounds.