今天就跟大家聊聊有关如何在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装饰器有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。