Python Object-Oriented programming--access control

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.