欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python property的使用技巧分享

 更新時間:2021年05月08日 10:21:03   作者:憶想不到的暉  
這篇文章主要介紹了python property的使用技巧分享,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下

property屬性

一種用起來像是使用實例屬性一樣的特殊屬性,可以對應(yīng)于某個方法

既要保護類的封裝特性,又要讓開發(fā)者可以使用 對象.屬性 的方式操作方法,@property 裝飾器,可以直接通過方法名來訪問方法,不需要在方法名后添加一對  () 小括號。

來看下求圓的面積的例子

class Circle(object):

    PI = 3.14

    def __init__(self, r):
        # r圓的半徑
        self.r = r
        self.__area = self.PI * self.r * self.r
    
    @property
    def area(self):
        return self.__area

    def get_area(self):
        return self.__area


In [2]: c = Circle(10)

In [3]: c.area
Out[3]: 314.0

In [4]: c.get_area()
Out[4]: 314.0

property屬性的定義和調(diào)用要注意一下幾點:

  • 定義時,在實例方法的基礎(chǔ)上添加 @property 裝飾器;并且僅有一個 self 參數(shù)
  • 調(diào)用時,無需括號 ()

實例方法:c.get_area()

property裝飾的方法:c.area

具體實例

對于某商城中顯示電腦主機的列表頁面,每次請求不可能把數(shù)據(jù)庫中的所有內(nèi)容都顯示到頁面上,而是通過分頁的功能局部顯示,所以在向數(shù)據(jù)庫中請求數(shù)據(jù)時就要顯示的指定獲取從第 m 條到第 n條的所有數(shù)據(jù) 這個分頁的功能包括:

  • 根據(jù)用戶請求的當(dāng)前頁和總數(shù)據(jù)條數(shù)計算出 m 和 n
  • 根據(jù) m 和 n 去數(shù)據(jù)庫中請求數(shù)據(jù)
class Pager(object):
    
    def __init__(self, current_page):
        
        # 用戶當(dāng)前請求的頁碼(第一頁、第二頁...)
        self.current_page = current_page
        
        # 每頁默認(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

# ipython測驗
In [2]: p = Pager(1)


In [3]: p.start		# 就是起始值,即:m
Out[3]: 0

In [4]: p.end		# 就是結(jié)束值,即:n
Out[4]: 10

In [5]: p = Pager(2)

In [6]: p.start
Out[6]: 10

In [7]: p.end
Out[7]: 20

property屬性的有兩種方式

  • 裝飾器 即:在方法上應(yīng)用裝飾器 @property
  • 類屬性 即:在類中定義值為 property 對象的類屬性 property()

裝飾器方式

在類的實例方法上應(yīng)用 @property 裝飾器

Python中的類有舊式類 和  新式類,新式類 的屬性比 舊式類的屬性豐富。

舊式類

舊式類,具有一種 @property 裝飾器

class Goods:
    
    def __init__(self, name):
        self.name = name
        
    @property
    def price(self):
        return 100
    
# ipython測驗
In [10]: g = Goods('手表')

In [11]: g.price
Out[11]: 100

新式類

新式類,具有三種 @property 裝飾器

class Goods:
    """
    python3中默認(rèn)繼承object類
    以python2、3執(zhí)行此程序的結(jié)果不同,因為只有在python3中才有@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')

        
# ipython測驗
In [13]: g = Goods()

In [14]: g.price
@property

In [15]: g.price = 100
@price.setter

In [16]: del g.price
@price.deleter
  • g.price  單獨調(diào)用自動執(zhí)行 @property 修飾的 price 方法,并獲取方法的返回值
  • g.price = 100 賦值自動執(zhí)行 @price.setter 修飾的 price 方法,并將 100 賦值給方法的參數(shù)
  • del g.price 刪除自動執(zhí)行 @price.deleter 修飾的 price 方法

注意

  • 舊式類中的屬性只有一種訪問方式,其對應(yīng)被 @property 修飾的方法
  • 新式類中的屬性有三種訪問方式,并分別對應(yīng)了三個被@property、@方法名.setter、@方法名.deleter 修飾的方法

由于新式類中具有三種訪問方式,我們可以根據(jù)它們幾個屬性的訪問特點,分別將三個方法定義為對同一個屬性:獲取、修改、刪除。

# Goods類@property應(yīng)用

class Goods(object):

    def __init__(self, name, price):
        # 原價
        self.original_price = price

        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        # 實際價格 = 原價 * 折扣
        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):
        print('刪除商品原價')
        del self.original_price

        
# ipython測驗
In [22]: g = Goods('小米手機', 2000)

In [23]: g.price
Out[23]: 1600.0

In [24]: g.price = 3000

In [25]: g.price
Out[25]: 2400.0

In [26]: del g.price
刪除商品原價

In [27]: g.price
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-27-38ee45b469f2> in <module>
----> 1 g.price

<ipython-input-18-d5ea66eb7ece> in price(self)
     12     def price(self):
     13         # 實際價格 = 原價 * 折扣
---> 14         new_price = self.original_price * self.discount
     15         return new_price
     16

AttributeError: 'Goods' object has no attribute 'original_price'

類屬性方式

創(chuàng)建值為 property 對象的類屬性,當(dāng)使用類屬性的方式創(chuàng)建 property 屬性時,舊式類 和 新式類無區(qū)別

class Foo:
    
    def get_bar(self):
        return 'get_bar'

    BAR = property(get_bar)
    
    
# ipython 測驗
In [32]: f = Foo()

In [33]: f.BAR
Out[33]: 'get_bar'

f.BAR 自動調(diào)用 get_bar() 方法,并獲取方法的返回值

property() 中有個四個參數(shù)

  • 第一個參數(shù)是方法名,調(diào)用 對象.屬性 時自動觸發(fā)執(zhí)行方法
  • 第二個參數(shù)是方法名,調(diào)用 對象.屬性 = XXX 時自動觸發(fā)執(zhí)行方法
  • 第三個參數(shù)是方法名,調(diào)用 del 對象.屬性 時自動觸發(fā)執(zhí)行方法
  • 第四個參數(shù)是字符串,調(diào)用 對象.屬性.__doc__ ,此參數(shù)是該屬性的描述信息
class Foo(object):

    def __init__(self, bar):
        self.bar = bar
    
    def get_bar(self):
        print('get_bar')
        return self.bar

    def set_bar(self, value): 
        """必須要有兩個參數(shù)"""
        print('set bar ' + value)
        self.bar = value

    def del_bar(self):
        print('del bar')
        del self.bar

    BAR = property(get_bar, set_bar, del_bar, "bar description...")

    
# ipython測驗
In [50]: f = Foo('python')

In [51]: f.BAR
get_bar
Out[51]: 'python'

In [52]: f.BAR = 'Java'
set bar Java

In [53]: f.BAR
get_bar
Out[53]: 'Java'

In [54]: del f.BAR
del bar

property對象與@property裝飾器對比

由于 類屬性方式 創(chuàng)建 property 對象屬性具有3種訪問方式,我們可以根據(jù)它們幾個屬性的訪問特點,分別將三個方法定義為對 同一個屬性:獲取、修改、刪除 ,跟 @property 裝飾器對比。

property對象類屬性

# Goods類 property對象類屬性 應(yīng)用

class Goods(object):

    def __init__(self, name, price):
        # 原價
        self.original_price = price

        # 折扣
        self.discount = 0.8

    def get_price(self):
        # 實際價格 = 原價 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    def set_price(self, value):
        self.original_price = value

    def del_price(self):
        print('刪除商品原價')
        del self.original_price

    PRICE = property(get_price, set_price, del_price, "price description")

    
# ipython測驗
In [59]: g = Goods('Mac電腦', 9000)

In [60]: g.PRICE
Out[60]: 7200.0

In [61]: g.PRICE = 10000

In [62]: g.PRICE
Out[62]: 8000.0

In [63]: del g.PRICE
刪除商品原價

@property裝飾器

# Goods類 @property裝飾器 應(yīng)用

class Goods(object):

    def __init__(self, name, price):
        # 原價
        self.original_price = price

        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        # 實際價格 = 原價 * 折扣
        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):
        print('刪除商品原價')
        del self.original_price
        
        
# ipython測驗
In [59]: g = Goods('Mac電腦', 9000)

In [60]: g.PRICE
Out[60]: 7200.0

In [61]: g.PRICE = 10000

In [62]: g.PRICE
Out[62]: 8000.0

In [63]: del g.PRICE
刪除商品原價

可以發(fā)現(xiàn)兩種都可以實現(xiàn)但 @property 裝飾器的在 舊式類中只有 @property , 沒有@method.setter 和

@method.deleter,新式類則兩種都可以使用。因此看大家的習(xí)慣,選一種。

以上就是python property的使用技巧分享的詳細(xì)內(nèi)容,更多關(guān)于python property的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python實現(xiàn)定制自動化業(yè)務(wù)流量報表周報功能【XlsxWriter模塊】

    Python實現(xiàn)定制自動化業(yè)務(wù)流量報表周報功能【XlsxWriter模塊】

    這篇文章主要介紹了Python實現(xiàn)定制自動化業(yè)務(wù)流量報表周報功能,結(jié)合實例形式分析了Python基于XlsxWriter模塊操作xlsx文件生成報表圖的相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Python常見異常的處理方式淺析

    Python常見異常的處理方式淺析

    異常指當(dāng)程序出現(xiàn)錯誤后程序的處理方法,異常機制提供了程序正常退出的安全通道.當(dāng)出現(xiàn)錯誤后,程序執(zhí)行的流程發(fā)生改變,程序的控制權(quán)轉(zhuǎn)移到異常處理器,如序列的下標(biāo)越界、打開不存在的文件、空引用異常等
    2023-02-02
  • python打印經(jīng)典故事從前有座山的幾種寫法

    python打印經(jīng)典故事從前有座山的幾種寫法

    在定義了函數(shù)之后,就可以使用該函數(shù)了,下面這篇文章主要給大家介紹了關(guān)于python打印經(jīng)典故事從前有座山的幾種寫法,通過這個有意思的實例幫助大家學(xué)習(xí)python,需要的朋友可以參考下
    2022-05-05
  • Pandas 中的join函數(shù)應(yīng)用實現(xiàn)刪除多余的空行

    Pandas 中的join函數(shù)應(yīng)用實現(xiàn)刪除多余的空行

    這篇文章主要介紹了Pandas 中的join函數(shù)應(yīng)用實現(xiàn)刪除多余的空行,str.join也就是sequence要連接的元素序列,下面我們來看看他的作用實現(xiàn)刪除多余的空行,需要的小伙伴可以參考一下
    2022-02-02
  • Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼

    Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼

    這篇文章主要介紹了Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Python中字典的相關(guān)操作介紹

    Python中字典的相關(guān)操作介紹

    大家好,本篇文章主要講的是Python中字典的相關(guān)操作介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • 使用python3實現(xiàn)操作串口詳解

    使用python3實現(xiàn)操作串口詳解

    本文給大家介紹的是在python中通過引用serial模塊包,來操作串口的方法的示例,有需要的小伙伴可以參考下
    2019-01-01
  • python獲取linux系統(tǒng)信息的三種方法

    python獲取linux系統(tǒng)信息的三種方法

    這篇文章主要介紹了python獲取linux系統(tǒng)信息的三種方法,幫助大家利用python了解自己的系統(tǒng)詳情,感興趣的朋友可以了解下
    2020-10-10
  • python 輸入字符串生成所有有效的IP地址(LeetCode 93號題)

    python 輸入字符串生成所有有效的IP地址(LeetCode 93號題)

    這篇文章主要介紹了python 生成所有有效的IP地址的方法,幫助大家解答題目,學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-10-10
  • Python 如何提高元組的可讀性

    Python 如何提高元組的可讀性

    這篇文章主要介紹了Python 如何提高元組的可讀性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08

最新評論