Demystifying Python Decorators

I've been curious about Python decorators but, until tonight, couldn't find an explanation that really made sense to me.


Python decorators modify functions, and in the case of class decorators, entire classes.


Decorators allow you to inject or modify code in functions or classes.^1^

Here's the basic syntax for using a decorator function>

~~~~ {.python name="code"} @theDecoratordef myFunction(): print "Inside myFunction" wzxhzdk:0

So let's take a look at how a decorator function actually looks:

~~~~ {.python name="code"} def theDecorator(f): def new_f(): print "Calling ", f.__name__ f() print "Done calling ", f.__name__ return new_f ~~~~

In the above case, we're replacing the decorated function myFunction with a new function that prints out some messages and calls the original myFunction

Given that definition of theDecorator and the decorated definition of myFunction, calling myFunction() gives the following output:


Calling myFunction

Inside myFunction

Done calling myFunction

This is, admittedly, a very simple example. For more details, check out the article in the footnotes or this is great article on decorators with arguments.

  1. [Decorators I: Introduction to Python Decorators][]