Demystifying Python metaprogramming

To metaprogram is to write a program that writes or manipulates itself or another program. It sounds much more intimidating than it is. In fact, metaprogramming has been a large part of two chapters in this book.

We were metaprogramming in Chapter 6, Automating Maya from the Outside, with our use of eval and exec to run arbitrary code. The following is an example of metaprogramming using the eval function. We evaluate a string to sum the numbers from 1 to 5.

>>> s = '+'.join([str(i) for i in range(1, 6)])
>>> s
>>> eval(s)

We were also metaprogramming when creating closures and decorators in Chapter 4, Leveraging Context Managers and Decorators in Maya. The following is an example of metaprogramming ...

Get Practical Maya Programming with Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.