Two-way conversions

suggest change

Descriptor objects can allow related object attributes to react to changes automatically.

Suppose we want to model an oscillator with a given frequency (in Hertz) and period (in seconds). When we update the frequency we want the period to update, and when we update the period we want the frequency to update:

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

We pick one of the values (frequency, in Hertz) as the “anchor,” i.e. the one that can be set with no conversion, and write a descriptor class for it:

class Hertz(object):
    def __get__(self, instance, owner):
        return self.value

    def __set__(self, instance, value):
        self.value = float(value)

The “other” value (period, in seconds) is defined in terms of the anchor. We write a descriptor class that does our conversions:

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)

Now we can write the Oscillator class:

class Oscillator(object):
    period = Second()  # Class attribute, so Second is used as a descriptor

    def __init__(self, freq):
        self.freq = Hertz()  # Stores a Hertz object on the instance
        self.freq = freq  # Replaces it with a float; Hertz is not used as a descriptor

period is a class attribute, so Second is invoked. self.freq = Hertz() does not install a descriptor: the next line replaces that object with a float, and Hertz.__get__ and Hertz.__set__ are never called. The transcript still matches, because Second reads and writes that float. A descriptor runs only when it is stored on the class. Hertz also keeps value on the descriptor object, so one Hertz on the class would be shared by every instance. The transcript above was checked on Python 3.14.6.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents