Injecting code into a function

Lonnie Princehouse finite.automaton at gmail.com
Mon Apr 25 13:45:47 EDT 2005


I expect you could combine the following with decorators as an easy way
to grab a function's locals just before it exits...  you could also use
exec or eval to execute code in the function's local namespace.
---------------------------------------

# Try it:

def trace_returns(frame, event, arg):
  if event == 'return':
    print "[frame locals just before return: %s]" % frame.f_locals
  return trace_returns

def foo(a, b):
  return a + b

import sys
sys.settrace(trace_returns)

foo(1,2)




More information about the Python-list mailing list