python中@property是什么
发布时间:2024-07-04 14:57:35

说明
1、Python内置@property装饰器负责将一种方法转换为属性调用。
2、@property广泛应用于类定义,可以让调用者写出简短的代码。
同时,确保对参数进行必要的检查,以便在序列运行中出错。
实例
classStudent(object):
@property
defscore(self):
returnself._score
@score.setter
defscore(self,value):
ifnotisinstance(value,int):
raiseValueError('scoremustbeaninteger!')
ifvalue<0orvalue>100:
raiseValueError('scoremustbetwen0~100!')
ifvalue<0orvalue>100:
raiseValueError('scoremustbetwen0~100!')
self._score=value以上是python中@property的介绍,希望对大家有所帮助。更多Python学习指导:python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。
下一篇 python实例如何绑定属性
