There are two different types of descriptors. Data descriptors are defined as objects that define both a __get__() and a __set__() method, whereas non-data descriptors only define a __get__() method. This distinction is important when considering overrides and the namespace of an instance's dictionary. If a data descriptor and an entry in an instance's dictionary share the same name, the data descriptor will take precedence. However, if instead a non-data descriptor and an entry in an instance's dictionary share the same name, the instance dictionary's entry will take precedence.
To make a read-only data descriptor, define both get() and set() with the set() raising an AttributeError when called. Defining the set() method with an exception raising placeholder is enough to make it a data descriptor.
An implemented example:
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:
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:
The "other" value (period, in seconds) is defined in terms of the anchor. We write a descriptor class that does our conversions:
Now we can write the Oscillator class:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0