Introduction
Python does not define member functions or properties like C + +, Java, C #, or keyword, which are known as public, private, or protected, and are identified by the conventions ' single underscore "_" and "__" double underscores as prefixes of functions or properties. There is a very big difference between using a single underline or a double underline.
1. A single underlined function or attribute. The ability to invoke and access in the class definition. Class can be directly interviewed. Can be interviewed in subclasses.
2. Double-underlined functions or properties that can be invoked and interviewed in the class definition. An instance of a class cannot be interviewed directly, and the subclass is inaccessible.
Note: For double-underlined functions or properties, the Python interpreter uses the method of name obfuscation, turning the private method "__method" into "_classname__method", with a detailed look at the examples below.
Private functions and properties for double underlines. Not visible in subclasses. There is no "overwrite"
Class Base (object): def __private (self): print (' Private value in Base ') def _protected (self): print ( ' protected value in base ') def public (self): print ("Public value in base") Self.__private () self._ Protected () class Derived (Base): def __private (self): print ("Override Private") def _protected (self): Print ("override protected") Print dir (Base) print ("=" *80) d = Derived () d.public () d._protected () d._derived__ Private () print ("=" *80) d.__private ()
The output results are as follows:
>>> [' _base__private ', ' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' _ _format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __ Repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' _protected ', ' Public ']========== ======================================================================<span style= "color: #FF0000;" >public value in baseprivate value in Baseoverride protected</span>override protected<span style= "color:# FF0000; " >override private</span>============================================================================== ==traceback (most recent call last): File "D:\temp\test.py", line, in <module> d.__private () Attributeerror: ' Derived ' object has no attribute ' __private ' >>>
Note that the red font portion of the output above may not be the same as the output we imagined. When calling the public method of a subclass, the subclass's double-underline method __private does not "overwrite" the parent class, but the _protected method of the subclass's single-underline method "Overrides" The parent class's method.
The reason for this is that subclasses want to "overwrite" the method of the parent class so that the subclass can have access to the parent class's corresponding method.
Do not define Python's obfuscation class method name
The Python interpreter for a class (ClassName) Double-underlined private method (__method), names are confused (name Mangle), and the rule is _classname__method. So do not exist __method and _classname__method at the same time in the class method.
Demo sample
Class Base (object): def _secret (self): print ("Base secret") def __hide (self): print ("Normal __hide" ) def _base__hide (self): print ("Special _base__hide") def Public (self): print ("from public Method") self.__hide () print dir (base ()) print ("=" *80) Base (). Public ()
Output such as the following
[<span style= "color: #FF0000;" > ' _base__hide ' </span>, ' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ' ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' _secret ', ' Public ']============================================== ==================================from public Method<span style= "color: #FF0000;" >special _base__hide</span>
Can see that __hide has been replaced by the _base__hide method. External can also be called directly through Base (). _base__hide () mode (but don't do it this way, it's confusing!). )。
Traps for Python class private methods