Python高級(jí)property屬性用法實(shí)例分析
本文實(shí)例講述了Python高級(jí)property屬性用法。分享給大家供大家參考,具體如下:
property屬性
1.property屬性:
是一個(gè)提高開(kāi)發(fā)者用戶體驗(yàn)度的屬性,可以將一個(gè)函數(shù)改造的像屬性一樣。
例:
# 定義的時(shí)候像是一個(gè)函數(shù) 使用的時(shí)候和屬性的方式是以樣的
class Foo(object):
@property
def money(self):
return 100
# f = Foo()
# m = f.money()
# print(m)
f = Foo()
print(f.money)
執(zhí)行結(jié)果:

2.property簡(jiǎn)單應(yīng)用:
例:根據(jù)當(dāng)前頁(yè)數(shù)和每頁(yè)顯示數(shù)據(jù)條數(shù),計(jì)算出該頁(yè)起始編號(hào)和結(jié)尾編號(hào)
class Pager:
def __init__(self, current_page):
# 用戶當(dāng)前請(qǐng)求的頁(yè)碼(第一頁(yè)、第二頁(yè)...)
self.current_page = current_page
# 每頁(yè)默認(rèn)顯示100條數(shù)據(jù)
self.per_items = 100
@property
def start(self):
val = (self.current_page - 1) * self.per_items + 1
return val
@property
def end(self):
val = self.current_page * self.per_items
return val
p = Pager(2)
print(p.start)
print(p.end)
執(zhí)行結(jié)果:

3.裝飾器方式:在方法上應(yīng)用裝飾器
三種@property裝飾器:
class Goods:
@property
def price(self):
print('@property')
@price.setter
def price(self, value):
print('@price.setter')
@price.deleter
def price(self):
print('@price.deleter')
# ############### 調(diào)用 ###############
obj = Goods()
obj.price # 自動(dòng)執(zhí)行 @property 修飾的 price 方法,并獲取方法的返回值
obj.price = 123 # 自動(dòng)執(zhí)行 @price.setter 修飾的 price 方法,并將 123 賦值給方法的參數(shù)
del obj.price # 自動(dòng)執(zhí)行 @price.deleter 修飾的 price 方法
例:
class Goods(object):
def __init__(self):
self.org_price = 1000 # 價(jià)格
self.discount = 0.7 # 折扣
@property
def price(self):
val = self.org_price * self.discount
# 返回價(jià)格*折扣
return val
@price.setter
def price(self, new_val):
# 接收val,將價(jià)格修改為val
self.org_price = new_val
@price.deleter
def price(self):
# 將折扣修改為1(刪掉折扣)
self.discount = 1
g = Goods()
print(g.price)
g.price = 2000
print(g.price)
del g.price
print(g.price)
執(zhí)行結(jié)果:

4.類(lèi)屬性方式:在類(lèi)中定義值為property對(duì)象的類(lèi)屬性
屬性名 = property(獲取, 修改, 刪除, 備注)
例:
class Goods(object):
def __init__(self):
self.org_price = 1000 # 價(jià)格
self.discount = 0.7 # 折扣
def get_price(self):
val = self.org_price * self.discount
# 返回價(jià)格*折扣
return val
def set_price(self, new_val):
# 接收new_val,將價(jià)格修改為new_val
self.org_price = new_val
def del_price(self):
# 將折扣修改為1(刪掉折扣)
self.discount = 1
price = property(get_price, set_price, del_price, "備注:價(jià)格")
g = Goods()
print(g.price) # 獲取商品價(jià)格
g.price = 2000 # 修改商品價(jià)格
print(g.price)
del g.price # 刪除商品折扣
print(g.price)
print(Goods.price.__doc__)
執(zhí)行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python圖片驗(yàn)證碼識(shí)別最新模塊muggle_ocr的示例代碼
這篇文章主要介紹了python圖片驗(yàn)證碼識(shí)別最新模塊muggle_ocr的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
django配置連接數(shù)據(jù)庫(kù)及原生sql語(yǔ)句的使用方法
這篇文章主要給大家介紹了關(guān)于django配置連接數(shù)據(jù)庫(kù),以及原生sql語(yǔ)句的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Python+tkinter制作經(jīng)典登錄界面和點(diǎn)擊事件
Tkinter是?Python?標(biāo)準(zhǔn)?GUI?庫(kù),簡(jiǎn)稱(chēng)?“Tk”;從本質(zhì)上來(lái)說(shuō),它是對(duì)?TCL/TK?工具包的一種?Python?接口封裝。本文將利用tkinter制作一個(gè)經(jīng)典的登錄界面和點(diǎn)擊事件,需要的可以參考一下2022-09-09
解決django 向mysql中寫(xiě)入中文字符出錯(cuò)的問(wèn)題
這篇文章主要介紹了解決django 向mysql中寫(xiě)入中文字符出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python使用pyautogui模塊實(shí)現(xiàn)自動(dòng)化鼠標(biāo)和鍵盤(pán)操作示例
這篇文章主要介紹了Python使用pyautogui模塊實(shí)現(xiàn)自動(dòng)化鼠標(biāo)和鍵盤(pán)操作,簡(jiǎn)單描述了pyautogui模塊的功能,并結(jié)合實(shí)例形式較為詳細(xì)的分析了Python使用pyautogui模塊實(shí)現(xiàn)鼠標(biāo)與鍵盤(pán)自動(dòng)化操作相關(guān)技巧,需要的朋友可以參考下2018-09-09
python將xml xsl文件生成html文件存儲(chǔ)示例講解
這篇文章主要介紹了python將xml、xsl文件轉(zhuǎn)成html文件存儲(chǔ)方法,大家參考使用吧2013-12-12

