Python's property _getter and setter methods [turn] Python_getter and setter methods

Python_getter and setter methods

 

When assigned to a property, the use of 实例.属性=属性值the way the property is clearly exposed, and can not be limited inspection of the property value, java provides setter and getter method, then python is how to do it? For more information, please refer to: Python study guide

Property assignment method

When bound property, if we go directly to the property exposed, although it is very simple to write, however, no way to check the parameters that you can easily change the results:

s = Student()
s.score = 9999

The above assignment is clearly inconsistent with the actual situation, in order to limit the scope of the score, by a set_score()set score method, and then by a get_score()to get results, so, in the set_score()method where you can check the parameters:

class Student(object):

    def get_score(self): return self._score def set_score(self, value): if not isinstance(value, int): raise ValueError('score must be an integer!') if value < 0 or value > 100: raise ValueError('score must between 0 ~ 100!') self._score = value

Now, for any instance of Student operation can not arbitrarily set the score:

>>>s = Student()
>>>s.set_score(60)
>>>s.get_score()
60
>>>s.set_score(9999)
Traceback (most recent call last):
  ...
ValueError: score must between 0 ~ 100!

However, the above method invocation and slightly complicated, not directly with the property so straightforward.

There is no check both parameters, and variables can be accessed in a similar class attribute such a simple way? For the pursuit of the perfect Python programmers, it is necessary to do!

Remember decorator (secorator) can add functionality to function? For the method of the class, its role as the decorator. Python's built- @propertydecorator is responsible for the property to become a method call:

class Student(object):
 @property def score(self): return self.__score  @score.setter def score(self, value): if not isinstance(value, int): raise ValueError('score must be an integer!!!') if value < 0 or value > 100: raise ValueError('score must between 0~100!!!') self.__score = score

@propertyThe realization of more complex, let's look at how to use. To become a property getter method, just add @propertyit at this time, @propertywhich in turn created another decorator @score.setter, is responsible for assigning attributes to become a setter method, so we have a property controlled operation:

>>> s = Student()
>>> s.score = 60 # OK,实际转化为s.set_score(60)
>>> s.score # OK,实际转化为s.get_score() 60 >>> s.score = 9999 Traceback (most recent call last): ... ValueError: score must between 0 ~ 100! 

Note that this magic @property, for instance, when we are operating, we know that this property is probably not directly exposed, but through getter and setter methods to achieve.

You can also define the read-only attribute, define only getter method, does not define a setter method is a read-only property:

class Student(object):

 @property def birth(self): return self._birth  @birth.setter def birth(self, value): self._birth = value  @property def age(self): return 2015 - self._birth

The above birthis a read-write property, and ageis a read-only property, because ageaccording to birththe current time is calculated.

summary

@propertyWidely used in the definition of class, allowing the caller to write a short code, while ensuring the necessary checks on the parameters, so that the program is running reduces the possibility of error.

class Person(object):
def __init__(self,age):
self.age=age

@property
def age(self):
return self._age

@age.setter
def age(self,age):
self._age=age

@property
def birth(self):
return 2017-self._age

p = Person(20)
>>>print(p.age)
>>>print(p.birth)

>>>p.birth = 1997
Attribute error #birth不存在setter方法

 

 

Transfer from

https://www.cnblogs.com/mzc1997/p/7663052.html

https://www.cnblogs.com/miqi1992/p/8343234.html

When assigned to a property, the use of 实例.属性=属性值the way the property is clearly exposed, and can not be limited inspection of the property value, java provides setter and getter method, then python is how to do it? For more information, please refer to: Python study guide

Property assignment method

When bound property, if we go directly to the property exposed, although it is very simple to write, however, no way to check the parameters that you can easily change the results:

s = Student()
s.score = 9999

The above assignment is clearly inconsistent with the actual situation, in order to limit the scope of the score, by a set_score()set score method, and then by a get_score()to get results, so, in the set_score()method where you can check the parameters:

class Student(object):

    def get_score(self): return self._score def set_score(self, value): if not isinstance(value, int): raise ValueError('score must be an integer!') if value < 0 or value > 100: raise ValueError('score must between 0 ~ 100!') self._score = value

Now, for any instance of Student operation can not arbitrarily set the score:

>>>s = Student()
>>>s.set_score(60)
>>>s.get_score()
60
>>>s.set_score(9999)
Traceback (most recent call last):
  ...
ValueError: score must between 0 ~ 100!

However, the above method invocation and slightly complicated, not directly with the property so straightforward.

There is no check both parameters, and variables can be accessed in a similar class attribute such a simple way? For the pursuit of the perfect Python programmers, it is necessary to do!

Remember decorator (secorator) can add functionality to function? For the method of the class, its role as the decorator. Python's built- @propertydecorator is responsible for the property to become a method call:

class Student(object):
 @property def score(self): return self.__score  @score.setter def score(self, value): if not isinstance(value, int): raise ValueError('score must be an integer!!!') if value < 0 or value > 100: raise ValueError('score must between 0~100!!!') self.__score = score

@propertyThe realization of more complex, let's look at how to use. To become a property getter method, just add @propertyit at this time, @propertywhich in turn created another decorator @score.setter, is responsible for assigning attributes to become a setter method, so we have a property controlled operation:

>>> s = Student()
>>> s.score = 60 # OK,实际转化为s.set_score(60)
>>> s.score # OK,实际转化为s.get_score() 60 >>> s.score = 9999 Traceback (most recent call last): ... ValueError: score must between 0 ~ 100! 

Note that this magic @property, for instance, when we are operating, we know that this property is probably not directly exposed, but through getter and setter methods to achieve.

You can also define the read-only attribute, define only getter method, does not define a setter method is a read-only property:

class Student(object):

 @property def birth(self): return self._birth  @birth.setter def birth(self, value): self._birth = value  @property def age(self): return 2015 - self._birth

The above birthis a read-write property, and ageis a read-only property, because ageaccording to birththe current time is calculated.

summary

@propertyWidely used in the definition of class, allowing the caller to write a short code, while ensuring the necessary checks on the parameters, so that the program is running reduces the possibility of error.

Guess you like

Origin www.cnblogs.com/paul8339/p/12150036.html