双向转换

描述符对象可以允许相关的对象属性自动响应更改。

假设我们想要建模具有给定频率(赫兹)和周期(以秒为单位)的振荡器。当我们更新频率时,我们希望更新周期,当我们更新周期时,我们希望频率更新:

 >>> 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