"I know this seems to go against the general idea of private and public" Not really "against", just different from C++ and Java.
Private -- as implemented in C++ and Java is not a very useful concept. It helps, sometimes, to isolate implementation details. But it is way overused.
Python names beginning with two __
are special and you should not, as a normal thing, be defining attributes with names like this. Names with __
are special and part of the implementation. And exposed for your use.
Names beginning with one _
are "private". Sometimes they are concealed, a little. Most of the time, the "consenting adults" rule applies -- don't use them foolishly, they're subject to change without notice.
We put "private" in quotes because it's just an agreement between you and your users. You've marked things with _
. Your users (and yourself) should honor that.
Often, we have method function names with a leading _
to indicate that we consider them to be "private" and subject to change without notice.
The endless getters and setters that Java requires aren't as often used in Python. Python introspection is more flexible, you have access to an object's internal dictionary of attribute values, and you have first class functions like getattr()
and setattr()
.
Further, you have the property()
function which is often used to bind getters and setters to a single name that behaves like a simple attribute, but is actually well-defined method function calls.