ITPub博客

首页 > IT职业 > IT图书 > python中@property是什么

python中@property是什么

IT图书 作者:dzone 时间:2021-09-11 09:49:10 0 删除 编辑

 说明

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

2、@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

以上就是python中@property的介绍,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3703/viewspace-2828966/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    76
  • 访问量
    534