1.1 Access Control1.1.1 External code modifies the properties of an instance
>>> Daidai = Student (' Daidai ', 99)
>>> Daidai.score
99
>>> Daidai.score = 1-- Modify score Properties
>>> Daidai.score
100
1.1.2 External Code Access instance property restrictions
>>> 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))
...
>>> Daidai = Student (' Daidai ', 99)
>>> Daidai.__name
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Student ' object has Noattribute ' __name '
in the in Python, the variable name of an instance begins with __ and becomes a private variable , whichcan only be accessed internally and cannot be accessed externally. Of course, the external code must not modify the properties of the internal instance at this time.
The method must be called when modifying the property variable
>>> class Student (object):
... def __init__ (self, Name,score):
... self.__name = name
... self.__score = Score
... def set_name (self, name):
... self.__name = name
... def set_score (self, score):
... self.__score = Score
... def print_score (self):
... print ('%s:%s '% (Self.__name, Self.__score))
...
>>>
>>>
>>> Daidai = Student (' Daidai ', 99)
>>> Daidai.print_score ()
daidai:99
>>> daidai.set_name (' Xiongxiong ')
>>> Daidai.print_score ()
xiongxiong:99
To get the variable property of a real example, you need to pass method
Class Student (object):
...
def get_name (self):
Return Self.__name
def get_score (self):
Return Self.__score
When a method is called to modify a variable property, the parameter check
>>> class Student (object):
... def __init__ (self, Name,score):
... self.__name = name
... self.__score = Score
... def set_name (self, name):
... self.__name = name
... def set_score (self, score):
... if 0 <= score <=:-- parameter Check control Section
... self.__score = Score
.. else:
... raise ValueError (' Bad score ')
... def print_score (self):
... print ('%s:%s '% (Self.__name, Self.__score))
...
>>>
>>> Daidai = Student (' Daidai ', 99)
>>> Daidai.print_score ()
daidai:99
>>> Daidai.set_score (-1)
Traceback (most recent):
File "<stdin>", line 1, in <module>
File "<stdin>", line one, in Set_score
Valueerror:bad Score
Attention:
__xxx__ Special variables that can be accessed
__xxx private variable, inaccessible
_xxx can be accessed, not recommended to write like this
>>> class Student (object):
... def __init__ (self, Name,score):
... self._name = name
... self._score = Score
... def set_name (self, name):
... self.__name = name
... def set_score (self, score):
... if 0 <= score <= 100:
... self.__score = Score
.. else:
... raise ValueError (' Bad score ')
... def print_score (self):
... print ('%s:%s '% (Self.__name, Self.__score))
...
>>>
>>>
>>> Daidai = Student (' Daidai ', 99)
>>> Daidai._name
' Daidai '
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1826204
Python Object-Oriented programming--access control