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 previous definition of the student class, the external code is free to modify the properties of an instance name score :
>>> bart = Student(‘Bart Simpson‘, 98)>>> bart.score98>>> bart.score = 59>>> bart.score59
If you want the internal properties to be inaccessible externally, you can add the name of the property with two underscores __ , and in Python, the variable name of the instance __ becomes a private variable (private), which can only be accessed internally and cannot be accessed externally, so 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)
After the change, for external code, there is no change, but it has been unable to access from outside 实例变量.__name and 实例变量.__score :
>>> bart = Student(‘Bart Simpson‘, 98)>>> bart.__nameTraceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: ‘Student‘ object has 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 to the student class get_name and get_score this method:
class Student(object): ... def get_name(self): return self.__name def get_score(self): return self.__score
What if I want to allow external code to modify score? You can add methods to the Student class set_score :
class Student(object): ... 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 set_score(self, score): if 0 <= score <= 100: self.__score = score else: raise ValueError(‘bad score‘)
It is important to note that in Python, the variable name is similar, which starts with a double underscore, and ends with a double underscore, which is a special variable that __xxx__ can be accessed directly, not a private variable, so __name__ __score__ the variable name cannot be used.
There are times when you see an instance variable name that starts with an underscore, such as an _name instance variable that can be accessed externally, but, as you see in the rules, when you look at a variable like this, the meaning is, "although I can be accessed, please treat me as a private variable and don't feel free to access it."
is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. cannot be accessed directly __name because the Python interpreter has changed the variable to the outside __name _Student__name , so you can still _Student__name access the __name variable by:
>>> bart._Student__name‘Bart Simpson‘
However, it is strongly recommended that you do not do this because different versions of the Python interpreter may __name change to different variable names.
All in all, Python itself has no mechanism to stop you from doing bad things, all on your own.
Python Access Restrictions