August 2018
Intermediate to advanced
332 pages
9h 12m
English
When we create the descriptor object in the class that is going to use it, we generally need the descriptor to know the name of the attribute it is going to be handling.
This attribute name is the one we use to read from and write to __dict__ in the __get__ and __set__ methods, respectively.
Before Python 3.6, the descriptor couldn't take this name automatically, so the most general approach was to just pass it explicitly when initializing the object. This works fine, but it has an issue in that it requires that we duplicate the name every time we want to use the descriptor for a new attribute.
This is what a typical descriptor would look like if we didn't have this method:
class DescriptorWithName: def __init__(self, ...