Home >Backend Development >Python Tutorial >How Private Are Python's 'Private' Methods, Really?
Python's "Private" Methods: Not As Impenetrable As They Seem
Despite the misconception that Python's "private" methods offer strong encapsulation, a closer examination reveals that they are not truly private. While prefixing a method with double underscores (__myPrivateMethod()) suggests a private scope, it actually creates a method with a modified name.
For instance, the following class demonstrates this behavior:
class MyClass: def myPublicMethod(self): print('public method') def __myPrivateMethod(self): print('this is private!!')
When instantiating an object of this class, one would expect to be unable to access the private method:
obj = MyClass() obj.myPublicMethod()
However, a subsequent dir(obj) call reveals the existence of a magically created method:
dir(obj) ['_MyClass__myPrivateMethod', '__doc__', '__module__', 'myPublicMethod']
Calling this method reveals the "private" data:
obj._MyClass__myPrivateMethod()
This naming convention is employed to prevent subclasses from inadvertently overriding private methods and attributes of their superclasses. However, it does not hinder deliberate access from the outside.
For example:
class Foo(object): def __init__(self): self.__baz = 42 def foo(self): print(self.__baz) class Bar(Foo): def __init__(self): super(Bar, self).__init__() self.__baz = 21 def bar(self): print(self.__baz) x = Bar() x.foo() # 42 x.bar() # 21 print(x.__dict__) # {'_Bar__baz': 21, '_Foo__baz': 42}
In conclusion, Python's "private" methods are not truly private, allowing subclasses to access them. This design choice prioritizes subclassing flexibility over strict encapsulation, but it also necessitates mindful use by developers to maintain code integrity.
The above is the detailed content of How Private Are Python's 'Private' Methods, Really?. For more information, please follow other related articles on the PHP Chinese website!