屬性與 @property 方法讓你的python更高效
一、用屬性替代 getter 或 setter 方法
以下代碼中包含手動(dòng)實(shí)現(xiàn)的 getter(get_ohms
) 和 setter(set_ohms
) 方法:
class OldResistor(object): def __init__(self, ohms): self._ohms = ohms self.voltage = 0 self.current = 0 def get_ohms(self): return self._ohms def set_ohms(self, ohms): self._ohms = ohms r0 = OldResistor(50e3) print(f'Before: {r0.get_ohms()}') r0.set_ohms(10e3) print(f'After: {r0.get_ohms()}') # => Before: 50000.0 # => After: 10000.0
這些工具方法有助于定義類的接口,使得開發(fā)者可以方便地封裝功能、驗(yàn)證用法并限定取值范圍。
但是在 Python 語言中,應(yīng)盡量從簡(jiǎn)單的 public 屬性寫起:
class Resistor(object): def __init__(self, ohms): self.ohms = ohms self.voltage = 0 self.current = 0 r1 = Resistor(50e3) print(f'Before: {r1.ohms}') r1.ohms = 10e3 print(f'After: {r1.ohms}') # => Before: 50000.0 # => After: 10000.0
訪問實(shí)例的屬性則可以直接使用 instance.property
這樣的格式。
如果想在設(shè)置屬性的同時(shí)實(shí)現(xiàn)其他特殊的行為,如在對(duì)上述 Resistor
類的 voltage
屬性賦值時(shí),需要同時(shí)修改其 current
屬性。
可以借助 @property
裝飾器和 setter
方法實(shí)現(xiàn)此類需求:
from resistor import Resistor class VoltageResistor(Resistor): def __init__(self, ohms): super().__init__(ohms) self._voltage = 0 @property def voltage(self): return self._voltage @voltage.setter def voltage(self, voltage): self._voltage = voltage self.current = self._voltage / self.ohms r2 = VoltageResistor(1e3) print(f'Before: {r2.current} amps') r2.voltage = 10 print(f'After: {r2.current} amps') Before: 0 amps After: 0.01 amps
此時(shí)設(shè)置 voltage 屬性會(huì)執(zhí)行名為 voltage 的 setter 方法,更新當(dāng)前對(duì)象的 current 屬性,使得最終的電流值與電壓和電阻相匹配。
@property 的其他使用場(chǎng)景
屬性的 setter
方法里可以包含類型驗(yàn)證和數(shù)值驗(yàn)證的代碼:
from resistor import Resistor class BoundedResistor(Resistor): def __init__(self, ohms): super().__init__(ohms) @property def ohms(self): return self._ohms @ohms.setter def ohms(self, ohms): if ohms <= 0: raise ValueError('ohms must be > 0') self._ohms = ohms r3 = BoundedResistor(1e3) r3.ohms = -5 # => ValueError: ohms must be > 0
甚至可以通過 @property
防止繼承自父類的屬性被修改:
from resistor import Resistor class FixedResistance(Resistor): def __init__(self, ohms): super().__init__(ohms) @property def ohms(self): return self._ohms @ohms.setter def ohms(self, ohms): if hasattr(self, '_ohms'): raise AttributeError("Can't set attribute") self._ohms = ohms r4 = FixedResistance(1e3) r4.ohms = 2e3 # => AttributeError: Can't set attribute
要點(diǎn)
- 優(yōu)先使用 public 屬性定義類的接口,不手動(dòng)實(shí)現(xiàn) getter 或 setter 方法
- 在訪問屬性的同時(shí)需要表現(xiàn)某些特殊的行為(如類型檢查、限定取值)等,使用 @property
- @property 的使用需遵循 rule of least surprise 原則,避免不必要的副作用
- 緩慢或復(fù)雜的工作,應(yīng)放在普通方法中
二、需要復(fù)用的 @property 方法
對(duì)于如下需求:
編寫一個(gè) Homework 類,其成績(jī)屬性在被賦值時(shí)需要確保該值大于 0 且小于 100。借助 @property 方法實(shí)現(xiàn)起來非常簡(jiǎn)單:
class Homework(object): def __init__(self): self._grade = 0 @property def grade(self): return self._grade @grade.setter def grade(self, value): if not (0 <= value <= 100): raise ValueError('Grade must be between 0 and 100') self._grade = value galileo = Homework() galileo.grade = 95 print(galileo.grade) # => 95
假設(shè)上述驗(yàn)證邏輯需要用在包含多個(gè)科目的考試成績(jī)上,每個(gè)科目都需要單獨(dú)計(jì)分。則 @property 方法及驗(yàn)證代碼就要重復(fù)編寫多次,同時(shí)這種寫法也不夠通用。
采用 Python 的描述符可以更好地實(shí)現(xiàn)上述功能。在下面的代碼中,Exam 類將幾個(gè) Grade 實(shí)例作為自己的類屬性,Grade 類則通過 __get__
和 __set__
方法實(shí)現(xiàn)了描述符協(xié)議。
class Grade(object): def __init__(self): self._value = 0 def __get__(self, instance, instance_type): return self._value def __set__(self, instance, value): if not (0 <= value <= 100): raise ValueError('Grade must be between 0 and 100') self._value = value class Exam(object): math_grade = Grade() science_grade = Grade() first_exam = Exam() first_exam.math_grade = 82 first_exam.science_grade = 99 print('Math', first_exam.math_grade) print('Science', first_exam.science_grade) second_exam = Exam() second_exam.science_grade = 75 print('Second exam science grade', second_exam.science_grade, ', right') print('First exam science grade', first_exam.science_grade, ', wrong') # => Math 82 # => Science 99 # => Second exam science grade 75 , right # => First exam science grade 75 , wrong
在對(duì) exam 實(shí)例的屬性進(jìn)行賦值操作時(shí):
exam = Exam() exam.math_grade = 40
Python 會(huì)將其轉(zhuǎn)譯為如下代碼:
Exam.__dict__['math_grade'].__set__(exam, 40)
而獲取屬性值的代碼:
print(exam.math_grade)
也會(huì)做如下轉(zhuǎn)譯:
print(Exam.__dict__['math_grade'].__get__(exam, Exam))
但上述實(shí)現(xiàn)方法會(huì)導(dǎo)致不符合預(yù)期的行為。由于所有的 Exam 實(shí)例都會(huì)共享同一份 Grade 實(shí)例,在多個(gè) Exam 實(shí)例上分別操作某一個(gè)屬性就會(huì)出現(xiàn)錯(cuò)誤結(jié)果。
second_exam = Exam() second_exam.science_grade = 75 print('Second exam science grade', second_exam.science_grade, ', right') print('First exam science grade', first_exam.science_grade, ', wrong') # => Second exam science grade 75 , right # => First exam science grade 75 , wrong
可以做出如下改動(dòng),將每個(gè) Exam 實(shí)例所對(duì)應(yīng)的值依次記錄到 Grade 中,用字典結(jié)構(gòu)保存每個(gè)實(shí)例的狀態(tài):
class Grade(object): def __init__(self): self._values = {} def __get__(self, instance, instance_type): if instance is None: return self return self._values.get(instance, 0) def __set__(self, instance, value): if not (0 <= value <= 100): raise ValueError('Grade must be between 0 and 100') self._values[instance] = value class Exam(object): math_grade = Grade() writing_grade = Grade() science_grade = Grade() first_exam = Exam() first_exam.math_grade = 82 second_exam = Exam() second_exam.math_grade = 75 print('First exam math grade', first_exam.math_grade, ', right') print('Second exam math grade', second_exam.math_grade, ', right') # => First exam math grade 82 , right # => Second exam math grade 75 , right
還有另外一個(gè)問題是,在程序的生命周期內(nèi),對(duì)于傳給 __set__
的每個(gè) Exam 實(shí)例來說,_values
字典都會(huì)保存指向該實(shí)例的一份引用,導(dǎo)致該實(shí)例的引用計(jì)數(shù)無法降為 0 從而無法被 GC 回收。
解決方法是將普通字典替換為 WeakKeyDictionary
:
from weakref import WeakKeyDictionary self._values = WeakKeyDictionary()
參考資料
以上就是屬性與 @property 方法讓你的python更高效的詳細(xì)內(nèi)容,更多關(guān)于python 屬性與 @property 方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python opencv實(shí)現(xiàn)任意角度的透視變換實(shí)例代碼
這篇文章主要介紹了python opencv實(shí)現(xiàn)任意角度的透視變換實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python動(dòng)態(tài)配置管理Dynaconf的實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Python動(dòng)態(tài)配置管理Dynaconf實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07代碼詳解django中數(shù)據(jù)庫設(shè)置
在本篇文章里小編給大家分享了關(guān)于django中數(shù)據(jù)庫設(shè)置的相關(guān)實(shí)例內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)下。2019-01-01Python簡(jiǎn)易圖形界面庫easygui對(duì)話框整理大全
這篇文章主要給大家介紹了關(guān)于Python簡(jiǎn)易圖形界面庫easygui對(duì)話框的相關(guān)資料,EasyGUI是一個(gè)用Python編寫的非常簡(jiǎn)易的GUI編程模塊,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01用Python寫腳本,實(shí)現(xiàn)完全備份和增量備份的示例
下面小編就為大家分享一篇用Python寫腳本,實(shí)現(xiàn)完全備份和增量備份的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04