I think you are confusing private functions/attributes with functions/attributes with double underscores before AND after the name. There is nothing wrong with accessing attributes with double underscores before and after the attribute. They are just labelled like that to avoid collisions, but it's legitimate to use, though something like __getattr__()
probably doesn't make much sense to call outside of the class. But __name__
and __class__
are both okay to access.
I believe what you are thinking of is prefixing a function or attribute with double underscore. This will mangle the name and is not meant to be used/called outside the class. The difference is that they don't have a double underscore suffix.
class T():
def __priv(self):
pass
def __special__(self):
pass
__attr = None
print(dir(T()))
You should see _T__priv
, _T__attr
, and __special__
. So the double underscore before AND after will not mangle. I'm not sure making your own __someattribute__
is the best idea, but the builtin ones for sure are available to you to use.