python裝飾器中@property屬性的使用解析
1. 什么是property屬性
一種用起來(lái)像是使用的實(shí)例屬性一樣的特殊屬性,可以對(duì)應(yīng)于某個(gè)方法
定義:
class Foo:
def func(self):
pass
# 定義property屬性
@property
def prop(self):
pass調(diào)用
foo_obj = Foo() foo_obj.func() # 調(diào)用實(shí)例方法 foo_obj.prop # 調(diào)用property屬性
property屬性的定義和調(diào)用要注意一下幾點(diǎn): 定義時(shí),在實(shí)例方法的基礎(chǔ)上添加 @property 裝飾器;并且僅有一個(gè)self參數(shù) 調(diào)用時(shí),無(wú)需括號(hào)
2. 簡(jiǎn)單的實(shí)例
對(duì)于商城中顯示商品的列表頁(yè)面,每次請(qǐng)求不可能把數(shù)據(jù)庫(kù)中的所有內(nèi)容都顯示到頁(yè)面上,而是通過分頁(yè)的功能局部顯示,所以在向數(shù)據(jù)庫(kù)中請(qǐng)求數(shù)據(jù)時(shí)就要顯示的指定獲取從第m條到第n條的所有數(shù)據(jù) 這個(gè)分頁(yè)的功能包括:
根據(jù)用戶請(qǐng)求的當(dāng)前頁(yè)和總數(shù)據(jù)條數(shù)計(jì)算出 m 和 n 根據(jù)m 和 n 去數(shù)據(jù)庫(kù)中請(qǐng)求數(shù)據(jù)
定義
class Pager:
def __init__(self, current_page):
# 用戶當(dāng)前請(qǐng)求的頁(yè)碼(第一頁(yè)、第二頁(yè)...)
self.current_page = current_page
# 每頁(yè)默認(rèn)顯示10條數(shù)據(jù)
self.per_items = 10
@property
def start(self):
val = (self.current_page - 1) * self.per_items
return val
@property
def end(self):
val = self.current_page * self.per_items
return val調(diào)用
p = Pager(1) p.start # 就是起始值,即:m p.end # 就是結(jié)束值,即:n
Python的property屬性的功能是:property屬性內(nèi)部進(jìn)行一系列的邏輯計(jì)算,最終將計(jì)算結(jié)果返回。
3. property屬性的有兩種方式
裝飾器 即:在方法上應(yīng)用裝飾器 類屬性 即:在類中定義值為property對(duì)象的類屬性
3.1 裝飾器方式
在類的實(shí)例方法上應(yīng)用@property裝飾器
Python中的類有經(jīng)典類和新式類,新式類的屬性比經(jīng)典類的屬性豐富。( 如果類繼object,那么該類是新式類 )
3.1.1經(jīng)典類,具有一種@property裝飾器
定義
class Goods:
@property
def price(self):
return "laowang"調(diào)用
obj = Goods() result = obj.price # 自動(dòng)執(zhí)行 @property 修飾的 price 方法,并獲取方法的返回值 print(result)
3.1.2新式類,具有三種@property裝飾器
#coding=utf-8
class Goods:
"""python3中默認(rèn)繼承object類
以python2、3執(zhí)行此程序的結(jié)果不同,因?yàn)橹挥性趐ython3中才有@xxx.setter @xxx.deleter
"""
@property
def price(self):
print('@property')
@price.setter
def price(self, value):
print('@price.setter')
@price.deleter
def price(self):
print('@price.deleter')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 方法
注意
- 經(jīng)典類中的屬性只有一種訪問方式,其對(duì)應(yīng)被 @property 修飾的方法
- 新式類中的屬性有三種訪問方式,并分別對(duì)應(yīng)了三個(gè)被@property、@方法名.setter、@方法名.deleter修飾的方法
- 由于新式類中具有三種訪問方式,我們可以根據(jù)它們幾個(gè)屬性的訪問特點(diǎn),分別將三個(gè)方法定義為對(duì)同一個(gè)屬性:獲取、修改、刪除
class Goods(object):
def __init__(self):
# 原價(jià)
self.original_price = 100
# 折扣
self.discount = 0.8
@property
def price(self):
# 實(shí)際價(jià)格 = 原價(jià) * 折扣
new_price = self.original_price * self.discount
return new_price
@price.setter
def price(self, value):
self.original_price = value
@price.deleter
def price(self):
del self.original_price
obj = Goods()
obj.price # 獲取商品價(jià)格
obj.price = 200 # 修改商品原價(jià)
del obj.price # 刪除商品原價(jià)3.2 類屬性方式,創(chuàng)建值為property對(duì)象的類屬性
當(dāng)使用類屬性的方式創(chuàng)建property屬性時(shí),經(jīng)典類和新式類無(wú)區(qū)別
class Foo:
def get_bar(self):
return 'laowang'
BAR = property(get_bar)
obj = Foo()
reuslt = obj.BAR # 自動(dòng)調(diào)用get_bar方法,并獲取方法的返回值
print(reuslt)property方法中有個(gè)四個(gè)參數(shù)
- 第一個(gè)參數(shù)是方法名,調(diào)用 對(duì)象.屬性 時(shí)自動(dòng)觸發(fā)執(zhí)行方法
- 第二個(gè)參數(shù)是方法名,調(diào)用 對(duì)象.屬性 = XXX 時(shí)自動(dòng)觸發(fā)執(zhí)行方法
- 第三個(gè)參數(shù)是方法名,調(diào)用 del 對(duì)象.屬性 時(shí)自動(dòng)觸發(fā)執(zhí)行方法
- 第四個(gè)參數(shù)是字符串,調(diào)用 對(duì)象.屬性.doc ,此參數(shù)是該屬性的描述信息
#coding=utf-8
class Foo(object):
def get_bar(self):
print("getter...")
return 'laowang'
def set_bar(self, value):
"""必須兩個(gè)參數(shù)"""
print("setter...")
return 'set value' + value
def del_bar(self):
print("deleter...")
return 'laowang'
BAR = property(get_bar, set_bar, del_bar, "description...")
obj = Foo()
obj.BAR # 自動(dòng)調(diào)用第一個(gè)參數(shù)中定義的方法:get_bar
obj.BAR = "alex" # 自動(dòng)調(diào)用第二個(gè)參數(shù)中定義的方法:set_bar方法,并將“alex”當(dāng)作參數(shù)傳入
desc = Foo.BAR.__doc__ # 自動(dòng)獲取第四個(gè)參數(shù)中設(shè)置的值:description...
print(desc)
del obj.BAR # 自動(dòng)調(diào)用第三個(gè)參數(shù)中定義的方法:del_bar方法由于類屬性方式創(chuàng)建property屬性具有3種訪問方式,我們可以根據(jù)它們幾個(gè)屬性的訪問特點(diǎn),分別將三個(gè)方法定義為對(duì)同一個(gè)屬性:獲取、修改、刪除
# 定義
class Goods(object):
def __init__(self):
# 原價(jià)
self.original_price = 100
# 折扣
self.discount = 0.8
def get_price(self):
# 實(shí)際價(jià)格 = 原價(jià) * 折扣
new_price = self.original_price * self.discount
return new_price
def set_price(self, value):
self.original_price = value
def del_price(self):
del self.original_price
PRICE = property(get_price, set_price, del_price, '價(jià)格屬性描述...')
# 調(diào)用
obj = Goods()
obj.PRICE # 獲取商品價(jià)格
obj.PRICE = 200 # 修改商品原價(jià)
del obj.PRICE # 刪除商品原價(jià)到此這篇關(guān)于python裝飾器中@property屬性的使用解析的文章就介紹到這了,更多相關(guān)python裝飾器的@property內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 掌握Python property裝飾器巧妙管理類的屬性
- Python中通過property設(shè)置類屬性的訪問
- 關(guān)于python中@property的使用方法
- 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 list的index()和find()的實(shí)現(xiàn)
這篇文章主要介紹了python list的index()和find()的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
分析總結(jié)Python數(shù)據(jù)化運(yùn)營(yíng)KMeans聚類
本文主要以 Python 使用 Keans 進(jìn)行聚類分析的簡(jiǎn)單舉例應(yīng)用介紹聚類分析,它是探索性數(shù)據(jù)挖掘的主要任務(wù),也是統(tǒng)計(jì)數(shù)據(jù)分析的常用技術(shù),用于許多領(lǐng)域2021-08-08
python遠(yuǎn)程調(diào)用rpc模塊xmlrpclib的方法
今天小編就為大家分享一篇python遠(yuǎn)程調(diào)用rpc模塊xmlrpclib的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-01-01
Python執(zhí)行系統(tǒng)命令的五種方式小結(jié)
在日常開發(fā)中,有時(shí)需要在Python腳本中執(zhí)行系統(tǒng)命令,Python有五種方式來(lái)執(zhí)行系統(tǒng)命令(推薦使用第五種),本文為大家整理了這五種方法的具體使用,希望對(duì)大家有所幫助2024-01-01
Python編程實(shí)現(xiàn)控制cmd命令行顯示顏色的方法示例
這篇文章主要介紹了Python編程實(shí)現(xiàn)控制cmd命令行顯示顏色的方法,結(jié)合實(shí)例形式分析了Python針對(duì)命令行字符串顯示顏色屬性相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
關(guān)于使用python對(duì)mongo多線程更新數(shù)據(jù)
這篇文章主要介紹了關(guān)于使用python對(duì)mongo多線程更新數(shù)據(jù),文中提供了詳細(xì)的代碼說明,實(shí)際使用時(shí),需要根據(jù)具體情況進(jìn)行調(diào)整和優(yōu)化,需要的朋友可以參考下2023-04-04

