python 中的@property的用法詳解
1.什么是property
簡單地說就是一個類里面的方法一旦被@property裝飾,就可以像調(diào)用屬性一樣地去調(diào)用這個方法,它能夠簡化調(diào)用者獲取數(shù)據(jù)的流程,而且不用擔(dān)心將屬性暴露出來,有人對其進行賦值操作(避免使用者的不合理操作)。需要注意的兩點是
- 調(diào)用被裝飾方法的時候是不用加括號的
- 方法定義的時候有且只能有self一個參數(shù)
>>> class Goods(): def __init__(self,unit_price,weight): self.unit_price = unit_price self.weight = weight @property def price(self): return self.unit_price * self.weight >>> lemons = Goods(7,4) >>> >>> lemons.price 28
上面通過調(diào)用屬性的方式直接調(diào)用到 price 方法,property把復(fù)雜的處理過程封裝到了方法里面去,取值的時候調(diào)用相應(yīng)的方法名即可。
2.property屬性定義的兩種方式
A、裝飾器方式
在類的方法上應(yīng)用@property裝飾器,即上面那種方式。
B、類屬性方式
創(chuàng)建一個實例對象賦值給類屬性
>>> class Lemons(): def __init__(self,unit_price=7): self.unit_price = unit_price def get_unit_price(self): return self.unit_price def set_unit_price(self,new_unit_price): self.unit_price = new_unit_price def del_unit_price(self): del self.unit_price x = property(get_unit_price, set_unit_price, del_unit_price) >>> fruit = Lemons() >>> >>> fruit.x #調(diào)用 fruit.x 觸發(fā) get_unit_price 7 >>> >>> fruit.x = 9 #調(diào)用 fruit.x = 9 觸發(fā) set_unit_price >>> >>> fruit.x 9 >>> >>> fruit.unit_price #調(diào)用 fruit.unit_price 觸發(fā) get_unit_price 9 >>> del fruit.x #調(diào)用 del fruit.x 觸發(fā) del_unit_price >>> >>> fruit.unit_price Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> l.unit_price AttributeError: 'Lemons' object has no attribute 'unit_price'
property方法可以接收四個參數(shù)
- 第一個參數(shù)是獲得屬性的方法名,調(diào)用 對象.屬性時自動觸發(fā)
- 第二個參數(shù)是設(shè)置屬性的方法名, 給屬性賦值時自動觸發(fā)
- 第三個參數(shù)是刪除屬性的方法名,刪除屬性時自動觸發(fā)
- 第四個參數(shù)是字符串,是屬性的描述文檔,調(diào)用對象.屬性.doc時觸發(fā)
3.用property代替getter和setter方法
>>>class Watermelon(): def __init__(self,price): self._price = price #私有屬性,外部無法修改和訪問 def get_price(self): return self._price def set_price(self,new_price): if new_price > 0: self._price = new_price else: raise 'error:價格必須大于零'
用property代替getter和setter
>>>class Watermelon(): def __init__(self,price): self._price = price @property #使用@property裝飾price方法 def price(self): return self._price @price.setter #使用@property裝飾方法,當(dāng)對price賦值時,調(diào)用裝飾方法 def price(self,new_price): if new_price > 0: self._price = new_price else: raise 'error:價格必須大于零' >>> watermelon = Watermelon(4) >>> >>> watermelon.price 4 >>> >>> watermelon.price = 7 >>> >>> watermelon.price 7
到此這篇關(guān)于python @property的用法的文章就介紹到這了,更多相關(guān)python @property的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 掌握Python property裝飾器巧妙管理類的屬性
- python裝飾器中@property屬性的使用解析
- Python中通過property設(shè)置類屬性的訪問
- 關(guān)于python中@property的使用方法
- Python?property裝飾器使用案例介紹
- Python深入分析@property裝飾器的應(yīng)用
- python中@Property屬性使用方法
- Python中property屬性的用處詳解
- Python裝飾器中@property使用詳解
- Python中關(guān)于property使用的小技巧
- Python的@property的使用
- 詳解Python裝飾器之@property
- Python property函數(shù)的具體使用
相關(guān)文章
Python實現(xiàn)簡單圖像縮放與旋轉(zhuǎn)
大家好,本篇文章主要講的是Python實現(xiàn)簡單圖像縮放與旋轉(zhuǎn),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Matplotlib之解決plt.savefig()保存多張圖片有重疊的問題
這篇文章主要介紹了Matplotlib之解決plt.savefig()保存多張圖片有重疊的問題,具有很好的參考價值,希望對大家有所幫助,2023-09-09Python操作SQLite/MySQL/LMDB數(shù)據(jù)庫的方法
這篇文章主要介紹了Python操作SQLite/MySQL/LMDB數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11Python GUI編程學(xué)習(xí)筆記之tkinter控件的介紹及基本使用方法詳解
這篇文章主要介紹了Python GUI編程學(xué)習(xí)筆記之tkinter控件的介紹及基本使用方法,結(jié)合實例形式詳細分析了Python GUI編程中tkinter控件的原理、用法及相關(guān)操作注意事項,需要的朋友可以參考下2020-03-03利用python實現(xiàn)簡單的循環(huán)購物車功能示例代碼
購物車對我們每位開發(fā)者來說應(yīng)該都不陌生,下面這篇文章主要給大家介紹了利用python實現(xiàn)簡單的循環(huán)購物車功能的相關(guān)資料,文中給出了詳細的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-07-07python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼
這篇文章主要介紹了python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01