Access Visibility
- Private variable, the variable name preceded by "__"
- Private can not be inherited, can not call outside, but you can call internally.
- If you have to use private variables, you can use dir (class ()) to view its real name.
- Private variables / functions within the class can be called directly.
- If you want to reflect a variable / function is particularly important that you can use the "_"
#私有
class A(object): def __init__(self): self.__a=1000 self.__b=100 def __B(self): print(self.__a) self.c() def __c(self): print(self.__a) A_=A() print(A_._A__a)
#装饰器的使用
class Name(object): def __init__(self,a,b): self.__a=a self.__b=b @property def A (self): print(self.__a) name=Name(00,200) name.A
#修改器的使用 class Name(object): def __init__(self,a,b): self.__a=a self._b=b @property def A(self): print(self.__a) @A.setter def A(self,a1): self.__A=a1 def play(self): print(self.__a) print(self.__b) name=Name(100,200) name.A=1000 name.play()
- Examples: Input three numbers (private), and find three numbers, which try to change the two numbers
class A(object): def __init__(self,a,b,c): self.__a=a self.__b=b self.__c=c @property def B(self): print(self.__a) @B.setter def B(self,num): self.__a=num def sum(self): print(self.__a+self.__b+self.__c) a=A(12,1,4) a.B=99 a.sum()
In python class is dynamic.
class Num(object): def __init__(self): self.a = 1000 def A(self): print(self.b) num = Num() num.b =1000000 # print(num.a) print(num.b) num.A()
class Person (Object): # define Person object can only bind _name, _age properties and --gender __slots__ is = ( ' the _name ' , ' _age ' , ' 's _gender') DEF the __init__ (Self, name, Age): Self. __name__ = name Self. __age = Age