Created
June 17, 2011 01:13
-
-
Save johnpena/1030675 to your computer and use it in GitHub Desktop.
Understanding decorators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @my_decorator | |
| def foo(bar): | |
| return bar | |
| # IS EXACTLY THE SAME AS | |
| def foo(bar): | |
| return bar | |
| foo = my_decorator(foo) | |
| # Example usage: | |
| def dec_function(orig): | |
| print "Decorating: %r" % (orig,) | |
| def inner(*args, **kwargs): | |
| print "Inner: %r %r" % ( args, kwargs ) | |
| return orig(*args, **kwargs) | |
| return inner | |
| @dec_function | |
| def foo(request, bar): | |
| return HttpResponse(bar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment