如何在python3.6中使用@property装饰器

发布时间:2021-03-24 15:17:13 作者:Leah
来源:亿速云 阅读:158

今天就跟大家聊聊有关如何在python3.6中使用@property装饰器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

具体如下:

1、@property装饰器的使用场景简单记录如下:

2、通过一个例子来加深对@property装饰器的理解:利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution。

代码实现如下:

class Screen(object):
 @property
 def width(self):
 return self._width
 @width.setter
 def width(self,value):
 self._width = value
 @property
 def height(self):
 return self._height
 @height.setter
 def height(self,values):
 self._height = values
 @property
 def resolution(self):
 return self._width * self._height
s = Screen()
s.width = 1024
s.height = 768
print('resolution = ',s.resolution)

运行结果:

resolution =  786432

看完上述内容,你们对如何在python3.6中使用@property装饰器有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Python中,关于@property装饰器
  2. 装饰器 使用@property

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python3.6 @property

上一篇:怎么在Springboot 2中调用外部Tomcat源码

下一篇:zTree树插件怎么在jQuery项目中使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》