@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 an attribute is derived from other attributes in the class, so the derived attribute will update whenever the source attributes is changed.(只讀的時候用到@property)
Make an attribute as property by defining it as a function and add the @property decorator before the fn definition.(用法是在需要被裝飾的函數前面加上@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的必要性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @staticmethod用法
- 下一篇: MySQL性能优化之char、varch