user-supplied locals dict for function execution?

Lonnie Princehouse finite.automaton at
Mon Mar 20 18:24:14 CET 2006

Can anyone think of a way to substitute a user-supplied dictionary as
the local dict for a function call?


def f():
   x = 5

d = {}

exec_function_in_dictionary( f, d )  # ???

print d["x"]      #  5


Right now, the way I'm doing this is to parse the function body out of
its source code and call exec on the body.  This has problems ---
notably that it only works on functions for which source code can be
found, also that the parsing is tricky (but I can fix that).

More information about the Python-list mailing list