Chapter 8. Metaprogramming, protocols, and more

 

This chapter covers

  • Python protocols
  • Dynamic attribute access
  • Metaprogramming with metaclasses
  • Advanced .NET interoperation

In this chapter, we’re going to look under the hood of the Python programming language. We’ve covered all the basic syntax and how to use classes from the .NET framework with IronPython. To make full use of Python, you need to know how to hook Python classes into the infrastructure that the language provides. As you write classes and libraries in Python, you’ll need to define how your objects take part in normal operations and interact with other objects. Much of this interaction is done through protocols, the magic methods that we’ve already briefly discussed.

We focus here on the use of protocols, including the metaprogramming capabilities of Python in the form of metaclasses. Metaclasses have a reputation for being something of a black art, but no book on Python would be complete without them. They can be used to achieve tasks that are much harder or even impossible with other approaches. In this part of the chapter, you’ll deepen your understanding of Python as you learn about the most important protocols.

8.1. Protocols instead of interfaces

8.2. Dynamic attribute access

8.3. Metaprogramming

8.4. IronPython and the CLR

8.5. Summary

sitemap