Python?property裝飾器使用案例介紹
1.property
裝飾器:裝飾器是在不修改被裝飾對(duì)象源代碼以及調(diào)用方式的前提下為被裝飾對(duì)象添加新功能的可調(diào)用對(duì)象
property是一個(gè)裝飾器,是用來(lái)綁定給對(duì)象的方法偽造成一個(gè)數(shù)據(jù)屬性
裝飾器property,可以將類中的函數(shù)“偽裝成”對(duì)象的數(shù)據(jù)屬性,對(duì)象在訪問(wèn)該特殊屬性時(shí)會(huì)觸發(fā)功能的執(zhí)行,然后將返回值作為本次訪問(wèn)的結(jié)果。
使用property有效地保證了屬性訪問(wèn)的一致性。另外property還提供設(shè)置和刪除屬性的功能
應(yīng)用場(chǎng)景:有的功能屬性聽(tīng)起來(lái)更像數(shù)據(jù)屬性,python則提供了一種裝飾器,可以將功能屬性偽裝成數(shù)據(jù)屬性
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.案例
"""
成人的BMI數(shù)值:
過(guò)輕:低于18.5
正常:18.5-23.9
過(guò)重:24-27
肥胖:28-32
非常肥胖, 高于32
體質(zhì)指數(shù)(BMI)=體重(kg)÷身高^(guò)2(m)
EX:70kg÷(1.75×1.75)=22.86
"""
案例一:
class People: def __init__(self, name, weight, height): self.name = name self.weight = weight self.height = height # 定義函數(shù)的原因1: # 1、從bmi的公式上看,bmi應(yīng)該是觸發(fā)功能計(jì)算得到的 # 2、bmi是隨著身高、體重的變化而動(dòng)態(tài)變化的,不是一個(gè)固定的值 # 說(shuō)白了,每次都是需要臨時(shí)計(jì)算得到的 # 但是bmi聽(tīng)起來(lái)更像是一個(gè)數(shù)據(jù)屬性,而非功能 @property def bmi(self): return self.weight / (self.height ** 2) obj1 = People('egon', 70, 1.83) print(obj1.bmi()) obj1.height=1.86 print(obj1.bmi()) print(obj1.bmi)
案例二:
''' 學(xué)習(xí)中遇到問(wèn)題沒(méi)人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流群:711312441 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書(shū)! ''' class People: def __init__(self, name): self.__name = name def get_name(self): return self.__name def set_name(self, val): if type(val) is not str: print('必須傳入str類型') return self.__name = val def del_name(self): print('不讓刪除') # del self.__name name=property(get_name,set_name,del_name) obj1=People('egon') # print(obj1.get_name()) # obj1.set_name('EGON') # print(obj1.get_name()) # obj1.del_name() # 人正常的思維邏輯 print(obj1.name) # # obj1.name=18 # del obj1.name
案例三:
class People: def __init__(self, name): self.__name = name @property def name(self): # obj1.name return self.__name @name.setter def name(self, val): # obj1.name='EGON' if type(val) is not str: print('必須傳入str類型') return self.__name = val @name.deleter def name(self): # del obj1.name print('不讓刪除') # del self.__name obj1=People('egon') # 人正常的思維邏輯 print(obj1.name) # # obj1.name=18 # del obj1.name
到此這篇關(guān)于Python property裝飾器使用案例介紹的文章就介紹到這了,更多相關(guān)Python property內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 掌握Python property裝飾器巧妙管理類的屬性
- python裝飾器中@property屬性的使用解析
- Python中通過(guò)property設(shè)置類屬性的訪問(wèn)
- 關(guān)于python中@property的使用方法
- Python深入分析@property裝飾器的應(yīng)用
- python 中的@property的用法詳解
- python中@Property屬性使用方法
- Python中property屬性的用處詳解
- Python裝飾器中@property使用詳解
- Python中關(guān)于property使用的小技巧
- Python的@property的使用
- 詳解Python裝飾器之@property
- Python property函數(shù)的具體使用
相關(guān)文章
YOLOv5改進(jìn)之添加CBAM注意力機(jī)制的方法
注意力機(jī)制最先被用在NLP領(lǐng)域,Attention就是為了讓模型認(rèn)識(shí)到數(shù)據(jù)中哪一部分是最重要的,為它分配更大的權(quán)重,獲得更多的注意力在一些特征上,讓模型表現(xiàn)更好,這篇文章主要給大家介紹了關(guān)于YOLOv5改進(jìn)之添加CBAM注意力機(jī)制的相關(guān)資料,需要的朋友可以參考下2022-11-11三個(gè)Python常用的數(shù)據(jù)清洗處理方式總結(jié)
這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)處理過(guò)程中三個(gè)主要的數(shù)據(jù)清洗說(shuō)明,分別是缺失值/空格/重復(fù)值的數(shù)據(jù)清洗,感興趣的小伙伴可以了解一下2022-12-12Python實(shí)現(xiàn)區(qū)間調(diào)度算法
區(qū)間調(diào)度算法是一種在給定的一組任務(wù)中,選擇盡可能多的相互不沖突的任務(wù)的算法,本文主要介紹了如何使用Python實(shí)現(xiàn)區(qū)間調(diào)度算法,有需要的可以參考下2024-10-10深入探究Python Numba庫(kù)編譯優(yōu)化利器
這篇文章主要為大家介紹了Python Numba庫(kù)編譯優(yōu)化利器深入探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python基于動(dòng)態(tài)規(guī)劃算法計(jì)算單詞距離
這篇文章主要介紹了Python基于動(dòng)態(tài)規(guī)劃算法計(jì)算單詞距離的方法,實(shí)例分析了Python動(dòng)態(tài)規(guī)劃算法的實(shí)現(xiàn)與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Python3.4學(xué)習(xí)筆記之常用操作符,條件分支和循環(huán)用法示例
這篇文章主要介紹了Python3.4常用操作符,條件分支和循環(huán)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python3.4常見(jiàn)的數(shù)學(xué)運(yùn)算、邏輯運(yùn)算操作符,條件分支語(yǔ)句,循環(huán)語(yǔ)句等功能與基本用法,需要的朋友可以參考下2019-03-03