Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Created August 28, 2010 22:40
Show Gist options
  • Select an option

  • Save inklesspen/555654 to your computer and use it in GitHub Desktop.

Select an option

Save inklesspen/555654 to your computer and use it in GitHub Desktop.
# 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