Created
August 28, 2010 22:40
-
-
Save inklesspen/555654 to your computer and use it in GitHub Desktop.
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
| # Please note, this is a simplified example that ignores | |
| # the function parameters. In practice, you should use | |
| # the decorator module or another method of maintaining | |
| # method signatures. | |
| def mydecorator(f): | |
| print "It would not be safe to use the session here." | |
| def decorated(): | |
| print "It would be safe to use the session here." | |
| return f() | |
| print "Once more, it would not be safe to use the session here." | |
| return decorated | |
| class MyController(BaseController): | |
| @mydecorator | |
| def do_something(self): | |
| return "Hello World" | |
| # The reason it's safe to use the session INSIDE the 'decorated' | |
| # function is because that function wraps around the do_something | |
| # method. The two lines outside of decorated are executed while | |
| # Python is parsing the MyController class. The line INSIDE is | |
| # executed during the request. You can only use the session | |
| # during a request. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment