Within class, there can be properties and methods, and external code can manipulate the data by directly invoking the instance variable, thus hiding the internal complex logic.
However, from the definition of the previous student class, the external code is free to modify the name, score properties of an instance:
>>> bart = Student (' Bart Simpson ', Bart.score) >>> Bart.score------
59
>>> Bart.score
59
If you want internal properties to not be externally accessible, you can add a two underscore to the name of the attribute. In Python, the instance's variable name, if it starts with __, becomes a private variable (private), accessible only internally and inaccessible outside, so We change 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, there is no change for the external code, but the instance variable cannot be accessed externally. __name and instance variables. __score:
>>> bart = Student (' Bart Simpson ', the ')
>>> bart.__name
traceback (most recent call last):
File "<stdin>", line 1, in <module>
attributeerror: ' Student ' object has no attribute ' __name '
This ensures that the external code cannot arbitrarily modify the state within the object, so that the code is more robust by the protection of access restrictions.
But what if the external code gets name and score? Methods such as Get_name and Get_score can be added to the student class:
Class Student (object): ...
def get_name (self): return
self.__name
def get_score (self): return
Self.__score
What if you want to allow external code modification score? You can add Set_score methods to the Student class:
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 of a lot of 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 <=: Self.__score
= score
else:
raise ValueError (' bad Score ')
Note that in Python, variable names like __xxx__, which begin with a double underscore and end with a double underscore, are special variables that can be accessed directly, not private variables, so you can't use __name__, __score__ Such a variable name.
Sometimes you'll see an instance variable name that starts with an underscore, for example _name, such an instance variable outside is accessible, but, according to the conventions, when you see such a variable, meaning, "although I can be accessed, but, please treat me as a private variable, do not randomly access."
Must the instance variable at the beginning of the double underline not be accessible from the outside? Not really. __name is not directly accessible because the Python interpreter has changed the __name variable to _student__name, so the __name variable can still be accessed through _student__name:
>>> bart._student__name
' Bart Simpson '
But it is highly recommended that you do not do this because different versions of the Python interpreter may change the __name to different variable names.
In general, Python itself has no mechanism to stop you from doing bad things, all by itself.