title: How can I get a Python decorator to run after the decorated function has completed?
body: <p>I want to use a decorator to handle auditing of various functions (mainly Django view functions, but not exclusively). In order to do this I would like to be able to audit the function <strong>post-execution</strong> - i.e. the function runs as normal, and if it returns without an exception, then the decorator logs the fact.</p>
  
  <p>Something like:</p>
  
  <pre><code>@audit_action(action='did something')
  def do_something(*args, **kwargs):
      if args[0] == 'foo':
          return 'bar'
      else:
          return 'baz'
  </code></pre>
  
  <p>Where <code>audit_action</code> would only run after the function has completed.</p>