雙向轉換
描述符物件可以允許相關的物件屬性自動響應更改。
假設我們想要建模具有給定頻率(赫茲)和週期(以秒為單位)的振盪器。當我們更新頻率時,我們希望更新週期,當我們更新週期時,我們希望頻率更新:
>>> oscillator = Oscillator(freq=100.0) # Set frequency to 100.0 Hz
>>> oscillator.period # Period is 1 / frequency, i.e. 0.01 seconds
0.01
>>> oscillator.period = 0.02 # Set period to 0.02 seconds
>>> oscillator.freq # The frequency is automatically adjusted
50.0
>>> oscillator.freq = 200.0 # Set the frequency to 200.0 Hz
>>> oscillator.period # The period is automatically adjusted
0.005
我們選擇其中一個值(頻率,以赫茲為單位)作為錨點,即可以在沒有轉換的情況下設定的值,併為其編寫描述符類:
class Hertz(object):
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = float(value)
其他值(週期,以秒為單位)是根據錨定義的。我們編寫了一個描述符類來完成轉換:
class Second(object):
def __get__(self, instance, owner):
# When reading period, convert from frequency
return 1 / instance.freq
def __set__(self, instance, value):
# When setting period, update the frequency
instance.freq = 1 / float(value)
現在我們可以寫出振盪器類了:
class Oscillator(object):
period = Second() # Set the other value as a class attribute
def __init__(self, freq):
self.freq = Hertz() # Set the anchor value as an instance attribute
self.freq = freq # Assign the passed value - self.period will be adjusted