Chapter Summary

In this chapter, we studied metaclasses and explored examples of them in action. Metaclasses allow us to tap into the class creation protocol of Python, in order to manage or augment user-defined classes. Because they automate this process, they may provide better solutions for API writers than manual code or helper functions; because they encapsulate such code, they may minimize maintenance costs better than some other approaches.

Along the way, we also saw how the roles of class decorators and metaclasses often intersect: because both run at the conclusion of a class statement, they can sometimes be used interchangeably. Class decorators and metaclasses can both be used to manage both class and instance objects, though each tool may present tradeoffs in some use cases.

Since this chapter covered an advanced topic, we’ll work through just a few quiz questions to review the basics (candidly, if you’ve made it this far in a chapter on metaclasses, you probably already deserve extra credit!). Because this is the last part of the book, we’ll forgo the end-of-part exercises. Be sure to see the appendixes that follow for Python changes, the solutions to the prior parts’ exercises, and more; the last of these includes a sampling of typical application-level programs for self-study.

Once you finish the quiz, you’ve officially reached the end of this book’s technical material. The next and final chapter offers some brief closing thoughts to wrap up the book at large. I’ll see you there in the Python benediction after you work through this final quiz.