python中@property是什么

459次阅读
没有评论

python中@property是什么

python中@property说明

1、内置的@property装饰器Python负责将一种方法转换为属性调用。

2、@property广泛应用于类的定义中,可以让调用者写出简短的代码。

同时保证对参数进行必要的检查,从而序运行中出错的可能性。

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
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-06-08发表,共计482字。
新手QQ群:570568346,欢迎进群讨论 Python51学习