June 2019
Beginner to intermediate
770 pages
19h 24m
English
Internally, Python uses non-data descriptors as part of the implementation for class methods and static methods. This is possible because a descriptor provides access to the owning class, as well as the instance.
We'll look at an example of a descriptor that updates the instance and also works with the filesystem to provide an additional side-effect to use of the descriptor.
For this example, we'll add a descriptor to a class that will create a working directory that is unique to each instance of a class. This can be used to cache state, or debugging history, or even audit information in a complex application.
Here's an example of an abstract class that might use a StateManager internally:
class PersistentState: ...Read now
Unlock full access