python 中的@property的用法詳解
1.什么是property
簡(jiǎn)單地說(shuō)就是一個(gè)類里面的方法一旦被@property裝飾,就可以像調(diào)用屬性一樣地去調(diào)用這個(gè)方法,它能夠簡(jiǎn)化調(diào)用者獲取數(shù)據(jù)的流程,而且不用擔(dān)心將屬性暴露出來(lái),有人對(duì)其進(jìn)行賦值操作(避免使用者的不合理操作)。需要注意的兩點(diǎn)是
- 調(diào)用被裝飾方法的時(shí)候是不用加括號(hào)的
- 方法定義的時(shí)候有且只能有self一個(gè)參數(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上面通過(guò)調(diào)用屬性的方式直接調(diào)用到 price 方法,property把復(fù)雜的處理過(guò)程封裝到了方法里面去,取值的時(shí)候調(diào)用相應(yīng)的方法名即可。
2.property屬性定義的兩種方式
A、裝飾器方式
在類的方法上應(yīng)用@property裝飾器,即上面那種方式。
B、類屬性方式
創(chuàng)建一個(gè)實(shí)例對(duì)象賦值給類屬性
>>> 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方法可以接收四個(gè)參數(shù)
- 第一個(gè)參數(shù)是獲得屬性的方法名,調(diào)用 對(duì)象.屬性時(shí)自動(dòng)觸發(fā)
- 第二個(gè)參數(shù)是設(shè)置屬性的方法名, 給屬性賦值時(shí)自動(dòng)觸發(fā)
- 第三個(gè)參數(shù)是刪除屬性的方法名,刪除屬性時(shí)自動(dòng)觸發(fā)
- 第四個(gè)參數(shù)是字符串,是屬性的描述文檔,調(diào)用對(duì)象.屬性.doc時(shí)觸發(fā)
3.用property代替getter和setter方法
>>>class Watermelon():
def __init__(self,price):
self._price = price #私有屬性,外部無(wú)法修改和訪問(wèn)
def get_price(self):
return self._price
def set_price(self,new_price):
if new_price > 0:
self._price = new_price
else:
raise 'error:價(jià)格必須大于零'用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)對(duì)price賦值時(shí),調(diào)用裝飾方法
def price(self,new_price):
if new_price > 0:
self._price = new_price
else:
raise 'error:價(jià)格必須大于零'
>>> watermelon = Watermelon(4)
>>>
>>> watermelon.price
4
>>>
>>> watermelon.price = 7
>>>
>>> watermelon.price
7到此這篇關(guān)于python @property的用法的文章就介紹到這了,更多相關(guān)python @property的用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python @property的用法及含義全面解析
- 介紹Python的@property裝飾器的用法
- 實(shí)例講解Python編程中@property裝飾器的用法
- Python 中@property的用法詳解
- Python @property裝飾器原理解析
- 詳解Python裝飾器之@property
- Python進(jìn)階之@property動(dòng)態(tài)屬性的實(shí)現(xiàn)
- python裝飾器中@property屬性的使用解析
- Python @property原理解析和用法實(shí)例
- Python如何使用@property @x.setter及@x.deleter
- Python @property使用方法解析
- python中@Property屬性使用方法
- python中@property注解的具體使用
相關(guān)文章
Python學(xué)習(xí)筆記之getattr和hasattr用法示例詳解
在Python中,hasattr()、getattr()和setattr()是一組內(nèi)置函數(shù),用于對(duì)對(duì)象的屬性進(jìn)行操作和查詢,這篇文章主要介紹了Python學(xué)習(xí)筆記之getattr和hasattr用法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-08-08
基于Python編寫(xiě)監(jiān)控視頻存儲(chǔ)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了如何基于Python編寫(xiě)一個(gè)監(jiān)控視頻存儲(chǔ)計(jì)算器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
深入探究Python Numba庫(kù)編譯優(yōu)化利器
這篇文章主要為大家介紹了Python Numba庫(kù)編譯優(yōu)化利器深入探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python中的標(biāo)準(zhǔn)庫(kù)html
html庫(kù)是用于解析HTML的一個(gè)工具,是python自帶的標(biāo)準(zhǔn)庫(kù)之一,今天通過(guò)本文給大家介紹下python中的標(biāo)準(zhǔn)庫(kù)html,感興趣的朋友一起看看吧2022-04-04
關(guān)于自動(dòng)化測(cè)試框架pytest的Fixture固件
這篇文章主要介紹了關(guān)于自動(dòng)化測(cè)試框架pytest的Fixture固件,Fixture它其實(shí)就是一些函數(shù),會(huì)在執(zhí)行測(cè)試方法/測(cè)試函數(shù)前后加載運(yùn)行它們,需要的朋友可以參考下2023-03-03
Python3加密解密庫(kù)Crypto的RSA加解密和簽名/驗(yàn)簽實(shí)現(xiàn)方法實(shí)例
這篇文章主要介紹了Python3加密解密庫(kù)Crypto的RSA加解密和簽名/驗(yàn)簽實(shí)現(xiàn)方法實(shí)例,需要的朋友可以參考下2020-02-02
使用Anaconda創(chuàng)建Pytorch虛擬環(huán)境的排坑詳細(xì)教程
PyTorch是一個(gè)開(kāi)源的Python機(jī)器學(xué)習(xí)庫(kù),基于Torch,用于自然語(yǔ)言處理等應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于使用Anaconda創(chuàng)建Pytorch虛擬環(huán)境的相關(guān)資料,需要的朋友可以參考下2022-12-12

