Today, I give you the second (and last) excerpt from my book, and it’s the last chapter. It brushes over some of the cooler examples of descriptors out there. It’s a short sample, and I feel kind of bad not giving you something juicier, but too much of the rest of the book has references to other chapters and discussed ideas, making it difficult to use as an excerpt.
Chapter 12: Other Uses of Descriptors In the World
Much of the usefulness of descriptors covered in this book was just using them as specialized properties. While this is one of the primary purposes of descriptors, it’s not all that they can do, though even the more innovative uses still largely serve that purpose.
This is probably the best-known library that uses descriptors for some of its stronger powers. When using the declarative mapping style for data classes, the use of the Column descriptor allows users to specify all sorts of database metadata about the column that the attribute represents, including the data type, column name, whether it’s a primary key, etc.
That Column class also has a ton of other methods that are used when creating queries around the data, such as the ordering methods,
__gt__(), etc. and what table it’s in.
The use is extremely innovative and powerful and it’s all thanks to descriptors that it can be as easy to use as it is.
For more information, visit its GitHub repository or check out the presentation the creator gave at EuroPython 2014.
Elk is Python library that is almost all descriptors, allowing for classes to be defined in a more strict fashion. Every attribute for instances is meant to be defined in the class with an
ElkAttribute descriptor. Some examples of what can be done with
- setting an attribute as required
- making lazy attributes
- delegate to the methods on the attribute
- make an attribute read-only
- automatic constructor creation
There are other features in the library, attempting to make the tedious parts of class definition a little easier, and they can be seen in its documentation.
This isn’t a specific instance out there, but rather a well-known use for descriptors. For example, if an attribute needs to be a string that follows a certain regex pattern, a descriptor can be created that takes the regex, and every time a value is set into the descriptor, it validates that the new value fits the validation.
There are a bunch of different validation descriptors that can be written that allow a class to maintain its invariants.
Now you’ve seen some really cool uses for descriptors. Also, this is the end of the book, so I suggest you go out there and make your own really awesome descriptors. Go, and make the Python community an even more awesome place.
Next week, I’ll be taking another look at Kotlin and discussing one of its coolest features while also showing how to do the best you can to recreate that same effect in Java and Python. Obviously, with no formal support, it won’t be as pretty in the latter two languages, but it’s an interesting exercise nonetheless.