# "Single underline" The starting member variable is called a protection variable, meaning that only class objects and subclass objects can access these variables themselves; # "Double underline" begins with a private member, meaning that only the class object can access it, and even the subclass object cannot access the data. Class pub (object): var1 = ' Hello ' _var2 = ' yes ' __var3 = ' hah ' def __init__ (self): self._a = 123 self.__b = 456 self.c = 789 def show_a_var (self): return Self._a def show_b_var (self): Return self.__b def show_var2 (self): return self._var2 def show_var3 (self): return self.__Var3class foo (Pub): passprint (PUB.VAR1) sl = pub () print (SL.C) print ( Sl.show_a_var ()) print (Sl.show_b_var ()) print (SL.SHOW_VAR2 ()) print (SL.SHOW_VAR3 ()) Fa = foo () print (fa._ VAR2) print (fa.var1) print (Fa.show_a_var ()) print (FA.SHOW_VAR2 ())
This article is from the Fresh Air Team blog, so be sure to keep this source http://freshair.blog.51cto.com/8272891/1930129
The meaning of single underline and double underline in Python class