Wednesday, 15 January 2014

events - Is there a way in python to trigger an action upon exiting a function -


I am in the process of adding a bit of code to a system that requires a specific function call upon exit Is a function Most of the code I am updating include many exit points, which are necessary for me to add a one-liner just before each of them. Somewhat unattractive

What I just would like to say, "when you exit this function, do this", much like the unmistakable module (which I have found so far), but on exiting the whole script instead of the function Can be triggered.

Is there any work that I can do in this way?

(The way I am using Python 2.7.3)

You can use the decorator. It's just a wrapper around the function: You can do whatever you call before or before.

  def do_after (func): def wrapper (* args, ** kwargs): func (* args, ** kwargs) call_my_extra_function () return cover   < P> and use it:  
  @do_after def my_view (requested): ...    

No comments:

Post a Comment