想要去了解python中的property函数
搜:
python property函数
参考:
看到示例如下:
1 2 3 4 5 6 7 8 9 10 11 | 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 = value |
对应的输出:
1 2 3 4 5 6 7 8 | >>> 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! |
转载请注明:在路上 » [整理]python中的property函数