Pseudo-Private properties
The variable __x defined in class Example automatically becomes _example__x, and the variable with double underscore before the variable is called a pseudo-private property, why is it called a pseudo-private attribute? Because this property is not private immutable, can still be called in the class name __ variable name to modify the property, why need a pseudo-private property to exist? Let's start with an example code to illustrate
1 classC1:2 defmeth1 (self):3Self. x=884 defmeth2 (self):5 Print(self.) X)6 7 classC2:8 defMetha (self):9Self. x=99Ten defMETHB (self): One Print(self.) X) A - classC3 (C1,C2): - Pass the -I=C3 () - i.meth1 () - I.metha () + - i.meth2 () + I.METHB () A Print(I.__dict__) at -i._c1__x=322#modifying Pseudo-private properties - Print(I.__dict__) - - # About - # About in #{' X ': - #{' X ': _c1__x ': 322}
If Class 1 and Class 2 were written by programmers A and B, respectively, when two classes were mixed together, self for each class. X Gets the value depends on the last assignment of the class, and does not realize its own meaning (two programmers to their own meth1 and Metha method of the assignment of X is to take a different value, rather than want the class to be mixed after the X value of the same), so at this time if the two people are using pseudo-private variables will not appear, as follows
1 classC1:2 defmeth1 (self):3Self.__x=884 defmeth2 (self):5 Print(self.)__x)6 7 classC2:8 defMetha (self):9Self.__x=99Ten defMETHB (self): One Print(self.)__x) A - classC3 (C1,C2): - Pass the -I=C3 () - i.meth1 () - I.metha () + - i.meth2 () + I.METHB () A Print(I.__dict__) ati._c1__x=322 - Print(I.__dict__) - - # the - # About - #{' _c2__x ': ' _c1__x ': in #{' _c2__x ': _c1__x ': 322}
Avoid potential variable name collisions in instances as above
Further examples
1 classSuper:2 defmethod (self):3 Print('This is method Super')4 5 classTool:6 def __method(self):7 Print('The is method Tool')8 defOther (self):9 Print('Calling Method __method')TenSelf.__method() One A classSub1 (tool,super): - defactions (self): - Self.method () the - classSub2 (Tool): - def __init__(self): -self.method=99 + Print(Self.method) - +i1=Tool () A I1.other () at Print('************') -I2=Sub1 () - i2.actions () - #search for class inheritance trees call method in the order that it was searched in the tool class and searched in class Super - #However, because the method in the class tool class is pseudo-private, it cannot be called outside the tool class, so the method is hidden in the tool class . - #and search in class Super, because the super class methods method is not privatized, and the super class method is called. in Print('-----------------') -I2.other ()#Search in the inheritance tree the other method is not privatized by the tool class, allowing the private method to be called through the other method in the class __method to Print('***************') +i3=Sub2 () - #The instantiation of the Sub2 method is no longer an override of method methods in the parent tool, but instead creates a new member variable the * #Calling Method __method $ #The is method ToolPanax Notoginseng # ************ - #This is method Super the # ----------------- + #Calling Method __method A #The is method Tool the # *************** + # About
Pseudo-private properties of Python