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

python?@property?裝飾器使用方法

 更新時(shí)間:2022年03月23日 09:08:08   作者:周兵  
這篇文章主要介紹了python?@property?裝飾器使用詳細(xì),使用property可以講類的方法變成同名屬性,使用起來更加簡潔,下文最后舉例說明詳情說明需要的小伙伴可以參考一下

一、property的裝飾器用法

先簡單上個(gè)小栗子說明:

class property(fget=None,fset=None,fdel=None,doc=None)

  • fget是用于獲取屬性值的函數(shù)
  • fset是用于設(shè)置屬性值的函數(shù)
  • fdel是用于刪除屬性值的函數(shù)
  • doc為屬性對象創(chuàng)建文檔字符串

使用property可以講類的方法變成同名屬性,使用起來更加簡潔,最后實(shí)例展示

使用時(shí)建議直接用property的裝飾器用法, 比較簡潔,下面是官方示例,利用@property裝飾方法x將其變成名稱相同的屬性, 通過fget, fset, fdel可以控制x的讀寫刪屬性.

class C:
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    #設(shè)置fset,注意x要與@property修飾的方法名稱相同
    @x.setter
    #這個(gè)fset名稱沒有限制,建議與x相同
    def x(self, value):
        self._x = value

    #設(shè)置fdel
    @x.deleter
    def x(self):
        del self._x

話不多說, 直接上例子, 在Man這個(gè)類中設(shè)置一個(gè)可讀可寫的birthYear屬性,一個(gè)可讀gender屬性,一個(gè)可讀可寫可刪的體重屬性,還有一個(gè)利用birthYear推算出的age屬性

""" @property, @*.setter"""
from datetime import datetime

class Man:
    def __init__(self, birthYear=None, gender=None, weight=None):
        """1.使用@property可以隱藏私有屬性,例如把'_birthYear'屬性隱藏,
             把property對象birthYear作為對外的屬性"""

        self._birthYear = birthYear
        self._gender = gender
        self._weight = weight

    @property
    def birthYear(self):
        return self._birthYear
    
    @birthYear.setter
    def birthYear(self, value):
       """2.可以在setter中對屬性輸入進(jìn)行限制或者其他操作"""
        assert not value > datetime.now().year, f'please input the right value!'
        self._birthYear = value

    @property
    """3.獲取年齡直接用obj.age,而不需寫成方法obj.age(), 更加自然方便"""
    def age(self):
        return datetime.now().year - self._birthYear

    @property
    def gender(self):
        return self._gender

    @property
    def weight(self):
        return self._weight
    
    @weight.setter
    def weight(self, value):
        self._weight = value

    @weight.deleter
    def weight(self):
        del self._weight

lilei = Man(1996, 'male', 180)
age = lilei.age
print('Lilei今年{}歲\n'.format(age))

# 設(shè)置性別
try:
    lilei.gender = 'female'
except:
    print('性別無法進(jìn)行更改!\n')

# 更新體重
print(f'lilei減肥前的體重:{lilei.weight}\n')
lilei.weight = 200
print(f'lilei減肥后的體重:{lilei.weight}\n')
print('lilei減肥失敗后一氣之下將體重信息刪了\n')
del lilei.weight
try:
    print('lilei的體重{}'.format(lilei.weight))
except:
    print('找不到lilei的體重信息!')

輸出結(jié)果:

Lilei今年25歲

性別無法進(jìn)行更改!

lilei減肥前的體重:180

lilei減肥后的體重:200

lilei減肥失敗后一氣之下將體重信息刪了

找不到lilei的體重信息!

二、舉例說明

現(xiàn)在讓我們直觀看一下python內(nèi)置property修飾器為什么會(huì)存在,可以用來解決什么問題?

先來一個(gè)具體的例子,定義一個(gè)表示攝氏度的class并且這個(gè)類包含一個(gè)從攝氏度轉(zhuǎn)換到華氏溫度的方法。

1.不用setter和getter方法的實(shí)現(xiàn)

# 初始版本
class Celsius:
    def __init__(self, temperature = 0):
        self.temperature = temperature

    def to_fahrenheit(self):
        return (self.temperature * 1.8) + 32

# 實(shí)例化
human = Celsius()
# 設(shè)置溫度
human.temperature = 37
# 獲取溫度值
print(human.temperature)
# 調(diào)用內(nèi)置方法將攝氏度轉(zhuǎn)化為華氏溫度
print(human.to_fahrenheit())

輸出結(jié)果:

37
98.60000000000001

2.使用setter和getter的實(shí)現(xiàn),增加溫度值輸入的限制

但是如果現(xiàn)在有人讓human.temperature = -300 , 我們知道攝氏溫度是不可能低于-273.15的,此時(shí)需要對溫度值進(jìn)行限制, 常規(guī)方法是設(shè)置一個(gè)溫度值的settergetter的方法,此時(shí)溫度值存放在_temperature 中

# 更新溫度限制的版本
class Celsius:
    def __init__(self, temperature=0):
        self.set_temperature(temperature)

    def to_fahrenheit(self):
        return (self.get_temperature() * 1.8) + 32

    # getter method
    def get_temperature(self):
        return self._temperature

    # setter method
    def set_temperature(self, value):
        if value < -273.15:
            raise ValueError("攝氏溫度不可能低于 -273.15 !")
        self._temperature = value

# 實(shí)例化
human = Celsius(37)

# 使用添加的getter獲取溫度值
print(human.get_temperature())

# 調(diào)用內(nèi)置方法將攝氏度轉(zhuǎn)化為華氏溫度
print(human.to_fahrenheit())

# 使用添加的setter對溫度值進(jìn)行設(shè)置
human.set_temperature(-300)

# Get the to_fahreheit method
print(human.to_fahrenheit())

毫無疑問,在設(shè)置溫度值等于-300的時(shí)候肯定會(huì)報(bào)錯(cuò),但是這個(gè)時(shí)候你可能發(fā)現(xiàn)設(shè)置和獲取溫度值的代碼發(fā)生變化而且更復(fù)雜 并且 你需要對Celsius類的初始化函數(shù)進(jìn)行更改,self.temperature = temperature self.set_temperature(temperature), 如果現(xiàn)在是一個(gè)擁有很多屬性的類, 一個(gè)一個(gè)去進(jìn)行這樣的更改是很麻煩的,而且可能會(huì)導(dǎo)致與這個(gè)類別相關(guān)的代碼出現(xiàn)錯(cuò)誤, 有沒有更好的實(shí)現(xiàn)方式呢?這個(gè)時(shí)候就該今天的主角property裝飾器出場了

3.利用property裝飾器實(shí)現(xiàn)的版本

# 使用 @property 裝飾器版本
class Celsius:
    def __init__(self, temperature=0):
        #這里的self.temperture是下面定義的property對線 temperatue
        #所以你會(huì)發(fā)現(xiàn)在初始化Celsius時(shí), 調(diào)用了temperature.setting方法
        self.temperature = temperature

    def to_fahrenheit(self):
        return (self.temperature * 1.8) + 32

    @property
    def temperature(self):
        print("獲取溫度值...")
        return self._temperature

    @temperature.setter
    def temperature(self, value):
        print("設(shè)置溫度值...")
        if value < -273.15:
           raise ValueError("攝氏溫度不可能低于 -273.15 !")
        self._temperature = value


# 用property的實(shí)現(xiàn)后獲取和設(shè)置溫度值與最初的版本一樣!
human = Celsius()
# 設(shè)置溫度
human.temperature = 37
# 獲取溫度值
print(human.temperature)
# 調(diào)用內(nèi)置方法將攝氏度轉(zhuǎn)化為華氏溫度
print(human.to_fahrenheit())

#測試溫度限制功能
human.temperature = -300

輸出結(jié)果:

設(shè)置溫度值...
設(shè)置溫度值...
獲取溫度值...
37
獲取溫度值...
98.60000000000001
設(shè)置溫度值...
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
d:\2.github\python_demo\016_decorator.py in <module>
     30 
     31 #測試溫度限制功能
---> 32 human.temperature = -300

d:\2.github\python_demo\016_decorator.py in temperature(self, value)
     16         print("設(shè)置溫度值...")
     17         if value < -273.15:
---> 18            raise ValueError("攝氏溫度不可能低于 -273.15 !")
     19         self._temperature = value
     20 

ValueError: 攝氏溫度不可能低于 -273.15 !

可以看到此時(shí)temperature設(shè)置有限制而且獲取和設(shè)置溫度值的代碼與初始版本一模一樣,也就是說代碼可以向后兼容

到此這篇關(guān)于python @property 裝飾器使用詳細(xì)的文章就介紹到這了,更多相關(guān)python @property 裝飾器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)簡單socket通信的方法

    python實(shí)現(xiàn)簡單socket通信的方法

    這篇文章主要介紹了python實(shí)現(xiàn)簡單socket通信的方法,結(jié)合實(shí)例形式分析了socket通信服務(wù)端與客戶端的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-04-04
  • python利用線程實(shí)現(xiàn)多任務(wù)

    python利用線程實(shí)現(xiàn)多任務(wù)

    這篇文章主要介紹了python利用線程實(shí)現(xiàn)多任務(wù),幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • opencv python在視屏上截圖功能的實(shí)現(xiàn)

    opencv python在視屏上截圖功能的實(shí)現(xiàn)

    OpenCV是一個(gè)基于BSD許可(開源)發(fā)行的跨平臺計(jì)算機(jī)視覺庫,可以運(yùn)行在Linux、Windows、Android和Mac OS操作系統(tǒng)上。這篇文章主要介紹了opencv python在視屏上截圖,需要的朋友可以參考下
    2020-03-03
  • 詳解python讀取和輸出到txt

    詳解python讀取和輸出到txt

    這篇文章主要介紹了python讀取和輸出到txt,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python手拉手教你爬取貝殼房源數(shù)據(jù)的實(shí)戰(zhàn)教程

    Python手拉手教你爬取貝殼房源數(shù)據(jù)的實(shí)戰(zhàn)教程

    隨著人工智能的不斷發(fā)展,機(jī)器學(xué)習(xí)這門技術(shù)也越來越重要,很多人都開啟了學(xué)習(xí)機(jī)器學(xué)習(xí),本文就介紹了機(jī)器學(xué)習(xí)的基礎(chǔ)內(nèi)容,了解python爬蟲,本文給大家分享Python爬取貝殼房源數(shù)據(jù)的實(shí)戰(zhàn)教程,感興趣的朋友一起學(xué)習(xí)吧
    2021-05-05
  • python看某個(gè)模塊的版本方法

    python看某個(gè)模塊的版本方法

    今天小編就為大家分享一篇python看某個(gè)模塊的版本方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python模擬實(shí)現(xiàn)高斯分布擬合

    Python模擬實(shí)現(xiàn)高斯分布擬合

    當(dāng)我們繪制一個(gè)數(shù)據(jù)集(如直方圖)時(shí),圖表的形狀就是我們所說的分布,最常見的連續(xù)值形狀是鐘形曲線,也稱為高斯分布或正態(tài)分布,下面我們就來利用Python模擬實(shí)現(xiàn)一下高斯分布吧
    2023-12-12
  • 簡單的Python調(diào)度器Schedule詳解

    簡單的Python調(diào)度器Schedule詳解

    這篇文章主要介紹了簡單的Python調(diào)度器Schedule詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python性能優(yōu)化技巧

    Python性能優(yōu)化技巧

    Python的批評者聲稱Python性能低效、執(zhí)行緩慢,但實(shí)際上并非如此:嘗試以下6個(gè)小技巧,可以加快Pytho應(yīng)用程序。
    2015-03-03
  • 基于python實(shí)現(xiàn)從尾到頭打印鏈表

    基于python實(shí)現(xiàn)從尾到頭打印鏈表

    這篇文章主要介紹了基于python實(shí)現(xiàn)從尾到頭打印鏈表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評論