Python内置的@property装饰器就是负责把一个方法变成属性调用的

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

这样@property下面的score就变成了一个属性

疑惑:为什么不直接__init__直接定义属性

 

相关文章:

  • 2021-10-09
  • 2021-09-21
  • 2021-08-29
猜你喜欢
  • 2021-10-28
  • 2021-11-27
  • 2021-05-17
  • 2021-06-12
  • 2021-07-28
  • 2021-04-22
相关资源
相似解决方案