| Start |
|---|
| Simple descriptor There are two different types of descriptors. Data descriptors are defined as objects that define both a 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: |
| Two-way conversions 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: |
Read Article
https://develop.horje.com/learn/1434/reference