Python does not enforce strict access control like some other languages (e.g., Java, C++). Instead, it relies on naming conventions to indicate the intended level of access.
private: Indicated with a double underscore "__". private
protected: Indicated with a single underscore "_". protected
Private members use name mangling to prevent accidental access, but they can still be accessed if necessary (e.g., instance._ClassName__private_var). On the other hand, Protected methods and variables just need the single underscore without name mangling.
class MyClass:
''' description of the class '''
def __init__(self, a):
self.a = a
return
def __repr__(self):
return f'{self.__class__.__name__}({self.a!r})'
def __private(self):
print('this is protected')
def _protected(self):
print('this is private')
def normal(self):
print('this is normal')
c = MyClass(100)
c.normal()
c._protected()
c._MyClass__private() # requires name mangling to gain access