A descriptor is a special object that controls access to attributes of another object. Both objects must be new-style classes, and the descriptor must be attached to the owner’s class object, rather than to an instance.
In the following example, the
my_descriptor instance will control
access to the
attribute member of
class my_descriptor(object): ... class my_class(object): attribute = my_descriptor() c = my_class() v = c.attribute # calls descriptor's __get__ method c.attribute = v # calls descriptor's __set__ method
The descriptor interface contains the following methods:
These methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in the class dictionary of another new-style class, known as the owner class. Descriptors can only be implemented as new-style classes themselves.
For details, see invoking-descriptors.