Chapter 9. Metaprogramming
One of the most important mantras of software development is âdonât repeat yourself.â
That is, any time you are faced with a problem of creating highly repetitive
code (or cutting or pasting source code), it often pays to look for a more elegant
solution. In Python, such problems are often solved under the category of âmetaprogramming.â In
a nutshell, metaprogramming is about creating functions and classes whose main goal is to
manipulate code (e.g., modifying, generating, or wrapping existing code). The
main features for this include decorators, class decorators, and metaclasses. However,
a variety of other useful topicsâincluding signature objects, execution of code with exec()
,
and inspecting the internals of functions and classesâenter the picture. The main
purpose of this chapter is to explore various metaprogramming techniques and to give
examples of how they can be used to customize the behavior of Python to your own whims.
9.1. Putting a Wrapper Around a Function
Problem
You want to put a wrapper layer around a function that adds extra processing (e.g., logging, timing, etc.).
Solution
If you ever need to wrap a function with extra code, define a decorator function. For example:
import
time
from
functools
import
wraps
def
timethis
(
func
):
'''
Decorator that reports the execution time.
'''
@wraps
(
func
)
def
wrapper
(
*
args
,
**
kwargs
):
start
=
time
.
time
()
result
=
func
(
*
args
,
**
kwargs
)
end
=
time
.
time
()
(
func
.
__name__
,
end
-
start
)
Get Python Cookbook, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.