Python用@property使类方法像属性一样访问
生活随笔
收集整理的這篇文章主要介紹了
Python用@property使类方法像属性一样访问
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
class Screen(object):@property #讀取with的值getter方法def width(self):return self._width@width.setter #設(shè)置with的值setter方法def width(self,value):self._width = value@property #讀取height的值getter方法def height(self):return self._height@height.setter #設(shè)置height的值setter方法def height(self,value):self._height = value'''根據(jù)with和height的值可得到分辨率resolution因此分辨率resolution是一個(gè)只讀屬性'''@propertydef resolution(self):return self._width*self._height
總結(jié)
以上是生活随笔為你收集整理的Python用@property使类方法像属性一样访问的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python: Json串反序列化为自定
- 下一篇: python爬虫简单的添加代理进行访问