What is metaprogramming?
Maybe we could find a good academic definition of metaprogramming, but this is a book that is more about good software craftsmanship than about computer science theory. This is why we will use our own informal definition of metaprogramming:
"Metaprogramming is a technique of writing computer programs that can treat themselves as data, so they can introspect, generate, and/or modify themselves while running."
Using this definition, we can distinguish between two major branches of metaprogramming in Python:
- Introspection-oriented metaprogramming: Focused on natural introspection capabilities of the language and dynamic definitions of functions and types
- Code-oriented metaprogramming: Metaprogramming focused on treating code as mutable data structures
Introspection-oriented metaprogramming concentrates on the language's ability to introspect its basic elements, such as functions, classes, or types, and to create...