Decorators change behavior

Decorators change behavior#

Python’s decorators are special markers that cause Python to treat your code differently. Decorators let you express common idioms easily, and they’re used all over Python code in the real world.

There are many reasons to use decorators; one example is to automatically cache the results of functions. Suppose we are defining factorial recursively; a naive implementation will make n calls on the input n. We can speed things up by trading space for time: we’ll use a dictionary to remember the result. Here’s a manual implementation:

Notice how few calls got made… nice! It was a lot of work, though, and now we have this awkward global fact_cache lying around.

It turns out, Python can do the caching for you automatically. Check it out:

We get the same output, and we didn’t have to create or manage our cache. Nice! There are other useful decorators in the functools package, but you’ll find other useful decorators in all kinds of places.