Summary

Descriptors are a more advanced feature in Python that push the boundaries, closer to metaprogramming. One of their most interesting aspects is how they make crystal-clear that classes in Python are just regular objects, and, as such, they have properties and we can interact with them. Descriptors are, in this sense, the most interesting type of attribute a class can have because its protocol facilitates more advanced, object-oriented possibilities.

We have seen the mechanics of descriptors, their methods, and how all of this fits together, making a more interesting picture of object-oriented software design. By understanding descriptors, we were able to create powerful abstractions that yield clean and compact classes. We have seen how to fix decorators that we want to apply to functions and methods, we have understood a lot more about how Python works internally, and how descriptors play such a core and critical role in the implementation of the language.

This study of how descriptors are used internally in Python should work as a reference to identify good uses of descriptors in our own code, with the goal of achieving idiomatic solutions.

Despite all of the powerful options that descriptors represent to our advantage, we have to keep in mind when to properly make use of them without over-engineering. In this line, we have suggested that we should reserve the functionality of descriptors for truly generic cases, such as the design of internal development APIs, libraries, or frameworks. Another important consideration along these lines is that, in general, we should not place business logic in descriptors, but rather logic that implements technical functionality to be used by other components that do contain business logic.

Speaking of advanced functionality, the next chapter also covers an interesting and in-depth topic: generators. On the face of it generators are rather simple (and most readers are probably already familiar with them), but what they have in common with descriptors is that they can also be complex, yield a more advanced and elegant design, and make Python a unique language to work with.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.163.27