(Flask框架学习笔记1)使用@property 发表于 2016-04-15 | 分类于 Python | 以下笔记部分参考廖雪峰的博客 Python的内置装饰器@property负责把一个方法变成属性调用的 12345678910111213class 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 = value 12345678>>> s = Student()>>> s.score = 60 # OK,实际转化为s.set_score(60)>>> s.score # OK,实际转化为s.get_score()60>>> s.score = 9999Traceback (most recent call last): ...ValueError: score must between 0 ~ 100!