日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@property的必要性

發布時間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @property的必要性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章[1]的作者提到:

What will we do if we need to control access to x, make it read-only or do something else to it? Won't we have to refactor everything to the getters and setters that we avoided?

意思是:

雖然我們看到@property在控制讀寫屬性,那豈不是直接__init__就好了呢?

不是的,因為__init__是所有的成員都是可以任意操作的。

?

[2]中的結論有(括號中是翻譯):

  • When to use @property decorator?
    When an attribute is derived from other attributes in the class, so the derived attribute will update whenever the source attributes is changed.(只讀的時候用到@property)
  • How to make a @property?
    Make an attribute as property by defining it as a function and add the @property decorator before the fn definition.(用法是在需要被裝飾的函數前面加上@property)
  • When to define a setter method for the property?
    Typically, if you want to update the source attributes whenever the property is set. It lets you define any other changes as well.("意思是兩個屬性如果一個需要修改,另外一個根據該屬性自動修改,那么前者需要setter,后者僅僅@property')
  • [3]提到:
    By using property() method, we can modify our class and implement the value constraint without any change required to the client code. So that the implementation is backward compatible.(意思是在不修改原有代碼的情況下就可以加入新的功能)

    ?

    ?

    個人理解:

    @property適用于變量極其多的場景,這個場景下,有些變量需要可寫,有些變量需要只讀,這個時候__init__無法滿足需要,@property應運而生。

    我們或許會反駁,如果是所有變量都可寫的情況呢?

    此時使用__init__也可以實現可讀可寫的效果啊,還要"@propery以及@xxx.setter"干嘛呢?

    確實如此,從讀寫效果上,此時"@propery以及@xxx.setter"和__init__是一致的,不考慮封裝的話,此時使用__init__與"@propery以及@xxx.setter"效果上是一致的。

    還有一個好處是,使用@propery的話,我們可以看到右下方的函數名都可以保持一致,

    讓代碼更加清晰,試想:

    左側你讀代碼還要把代碼名字理解一遍,

    右側所有成員函數都是def value,掃一眼就知道都是在服務于value這個參數

    附錄是上面貼圖中左右兩側的代碼

    #---------------------------------------------------------附錄中是[3]中的代碼-------------------------------------------------------------------------

    不使用[3]

    # Python program to explain property() function # Alphabet class class Alphabet: def __init__(self, value): self._value = value # getting the values def getValue(self): print('Getting value') return self._value # setting the values def setValue(self, value): print('Setting value to ' + value) self._value = value # deleting the values def delValue(self): print('Deleting value') del self._value value = property(getValue, setValue, delValue, ) # passing the value x = Alphabet('GeeksforGeeks') print(x.value) x.value = 'GfG'del x.value

    使用[3]

    # Python program to explain property() # function using decorator class Alphabet: def __init__(self, value): self._value = value # getting the values @propertydef value(self): print('Getting value') return self._value # setting the values @value.setter def value(self, value): print('Setting value to ' + value) self._value = value # deleting the values @value.deleter def value(self): print('Deleting value') del self._value # passing the value x = Alphabet('Peter') print(x.value) x.value = 'Diesel'del x.value

    #------------------------------------------------------------------------------------------------------------------------------------------

    [1]What's the point of properties in Python?

    [2]Python @Property Explained – How to Use and When? (Full Examples)

    [3]Python | property() function

    總結

    以上是生活随笔為你收集整理的@property的必要性的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。