1. Private Properties#double underline before attribute #to protect property security #a private property cannot be called directly through an object, and a calling method needs to be added to invoke it; classpeople (object):def __init__(self): self.__name='Mic' defget_private_attr (Self, new_name): Self.__name=new_namePrint(self.)__name) #Creating Objectsp =people ()#Get private PropertyP.get_private_attr ('Lily') Print(p.__name) 2. Private Methods#Similarly, calls require another method to invoke a private method; classpeople (object):def __init__(self): self.__name='Mic' def __get_name(self):Print(self.)__name) defget_name (self): self.__get_name() P=people ()#P.__get_name ()P.get_name ()------------------------------------------------->>>Pseudo-Private#!! In fact, the Python class is not permission control, the so-called double underline is private, in fact, can be accessed; classPrivate (object):def __inaccesible(self):Print('you don't have access to') definaccesible (self):Print('Actually, you can see that.') self.__inaccesible() P=Private ()#p.__inaccesible () #p.inaccesible ()p._private__inaccesible ()>>>you don't have access toclassPrivate (object):def __init__(self): self.__name='Mic' defget_name (self):Print(self.)__name) P=Private ()#print (p.__name) #P.get_name () Print(P._private__name)>>>'Mic'
Pseudo-Private Properties | Pseudo-Private Methods | Python