在之前,我们介绍过装饰器的用法:
    装饰器

    装饰器可以动态给函数增加功能,对于class,Python内置的@property装饰器也能对 对象的属性进行增强

    1. class Student(object):
    2. @property
    3. def score(self):
    4. return self._score
    5. @score.setter
    6. def score(self, value):
    7. if not isinstance(value, int):
    8. raise ValueError('score must be an integer!')
    9. if value < 0 or value > 100:
    10. raise ValueError('score must between 0 ~ 100!')
    11. self._score = value
    1. >>> s = Student()
    2. >>> s.score = 60
    3. >>> s.score
    4. 60
    5. >>> s.score = 9999
    6. Traceback (most recent call last):
    7. ...
    8. ValueError: score must between 0 ~ 100!