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電子書!
'''
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)文章
python使用psutil模塊獲取系統(tǒng)狀態(tài)
作為程序猿,大家可能都熟悉linux系統(tǒng)的基礎(chǔ)信息獲取方法都是通過(guò)shell來(lái)獲取,但是在python中,我們還可以使用psutil模塊來(lái)獲取系統(tǒng)信息。psutil模塊把shell查看系統(tǒng)基礎(chǔ)信息的功能都包裝了下,使用更加簡(jiǎn)單,功能豐富。2016-08-08
pandas重置索引標(biāo)簽的實(shí)現(xiàn)示例
在使用Pandas進(jìn)行數(shù)據(jù)處理時(shí),有時(shí)候我們可能會(huì)需要對(duì)數(shù)據(jù)進(jìn)行重置索引的操作,本文主要介紹了pandas重置索引標(biāo)簽的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
python sleep和wait對(duì)比總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python sleep和wait對(duì)比總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。2021-02-02
Python實(shí)現(xiàn)復(fù)雜的事件驅(qū)動(dòng)架構(gòu)
事件驅(qū)動(dòng)架構(gòu)(Event-Driven?Architecture,?EDA)是一種軟件設(shè)計(jì)模式,它基于事件的產(chǎn)生、傳播和處理進(jìn)行系統(tǒng)的構(gòu)建,下面我們來(lái)看看如何在?Python?中實(shí)現(xiàn)復(fù)雜的事件驅(qū)動(dòng)架構(gòu)吧2024-12-12
Python將xml和xsl轉(zhuǎn)換為html的方法
這篇文章主要介紹了Python將xml和xsl轉(zhuǎn)換為html的方法,實(shí)例分析了使用libxml2模塊操作xml和xsl轉(zhuǎn)換為html的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

