美文网首页
python 使用@property

python 使用@property

作者: 起名字真难难难 | 来源:发表于2022-04-28 15:12 被阅读0次
class Student():
    @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
s=Student()
s.score=100
print(s.score)
运行结果

比get、set方法实现起来更简单

相关文章

网友评论

      本文标题:python 使用@property

      本文链接:https://www.haomeiwen.com/subject/ruldyrtx.html