How does the name mangling for a private class (class __ThisClass: pass) works, if we want to use the class inside a @staticmethod
I am able to access it with __class__ (The class to which a class instance belongs). However, is there a way to use a mangled name?
class PublicClass:
@staticmethod
def foo(n):
if n>10:
return n
return PublicClass.foo(100) # <---
pub_obj = PublicClass()
print(pub_obj.foo(1), pub_obj.foo(11))
output: 100 11
Here PublicClass.foo(100) can be used (or even __class__)
class __PrivateClass:
@staticmethod
def foo(n):
if n>10:
return n
return __class__.foo(100) # <---
pri_obj = __PrivateClass()
print(pri_obj.foo(1), pri_obj.foo(11))
output: 100 11
if we use __PrivateClass.foo(100) below exception is raised
class __PrivateClass:
@staticmethod
def foo(n):
if n>10:
return n
return __PrivateClass.foo(100) # <---
NameError: name '_PrivateClass__PrivateClass' is not defined
if we use _PrivateClass__PrivateClass.foo(100) below exception is raised (replace the line# <---)
NameError: name '_PrivateClass__PrivateClass' is not defined
Ask:
- How does the name mangling works for a private class?
- Should we (try to) never use the double underscore for the class name?
Note: Tried on Python 3.6.13 and Python 3.8.12