This article to share the content is about the Python object-oriented access restrictions, has a certain reference value, the need for friends can refer to
Inside class, you can have properties and methods, and external code can manipulate the data by invoking the instance variable directly, thus hiding the complex logic inside.
However, from the definition of the previous student class, the external code is free to modify the name and score properties of an instance:
Class Student (object): def __init__ (self,name,score): self.name = name Self.score = Score def print_ Score (self): print ('%s:%s '% (Self.name,self.score)) def get_grade (self): if Self.score >=90: Return ' A ' elif self.score>=600: return ' B ' else: return ' C ' Bart = Student (' Boyuan Zhou ', +) print (Bart.score) bart.score=120print (Bart.score)
100120
If you want the internal properties to be outside access, you can add the name of the property with two underscore __, in Python, the variable name of the instance will be a private property if it starts with __ (private), and can only be accessed internally and cannot be accessed externally. We changed the Student class:
Class Student (object): def __init__ (self,name,score): self.__name = name Self.__score = Score def Print_score (self): print ('%s:%s '% (Self.__name,self.__score)) def get_grade (self): if Self.__score >=90: return ' A ' elif self.__score>=600: Return ' B ' else: return ' C '
After the change, there is no change to the external code, but the instance variable cannot be accessed externally. __name and instance variables. __score:
Bart = Student (' Boyuan Zhou ', +) print (Bart.__name) Traceback (most recent call last): File "f:/mycompany/ mycompany/schooldemo.py ", line <module> print (bart.__name) attributeerror: ' Student ' object have no Attribute ' __name '
This ensures that external code cannot arbitrarily modify the state inside the object, so that the code is more robust through access-restricted protection.
But what if external code gets name and score? You can add methods such as Get_name and Get_score to the student class:
Class Student (object): def __init__ (self,name,score): self.__name = name Self.__score = score def get _name (self): return self.__name def get_score (self): return self.__score bart = Student (' Boyuan Zhou ', +) print (Bart.get_name ()) #Boyuan Zhou
What if I want to allow external code to modify score? You can add the Set_score method to the student class again:
Class Student (object): def __init__ (self,name,score): self.__name = name Self.__score = Score def Get_name (self): return self.__name def get_score (self): return self.__score def set_score (self , score): Self.__score=score
You may ask, the original kind of direct through Bart.score = 59 can also be modified Ah, why to define a method big trouble? Because in a method, you can check the parameters to avoid passing in invalid arguments:
Class Student (object): def __init__ (self,name,score): self.__name = name Self.__score = score def get _name (self): return self.__name def get_score (self): return self.__score def set_score (Self,score) : if 0 <=score <=: self.__score=score Else: raise ValueError (' Bad score ')
It is important to note that in Python, the variable name is similar to __xxx___, which starts with a double underscore, and ends with a double underscore, which is a special variable that can be accessed directly, not a private variable, so you cannot use __name__, __score__ Such a variable name.
There are times when you see an instance variable name that starts with an underscore, such as _name, which is accessible outside of an instance variable, but, according to the rules, when you see such a variable, the meaning is, ' although I may be accessed, please treat me as a private variable and not be arbitrarily accessed. '
is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. The __name cannot be accessed directly because the Python interpreter has changed the __name variable to _student__name, so the __name variable can still be accessed through _student_name:
Class Student (object): def __init__ (self,name,score): self.__name = name Self.__score = score def get _name (self): return self.__name def get_score (self): return self.__score def set_score (Self,score) : if 0 <=score <=: self.__score=score Else: raise ValueError (' Bad score ') Bart = Student (' Boyuan Zhou ', +) print (bart._student__name) print (Bart.get_name ()) #Boyuan Zhou#boyuan Zhou
However, it is highly recommended not to do this because different versions of the Python interpreter may change the __name to a different variable name.