Inferring initial locals()
George Sakkis
george.sakkis at gmail.com
Thu Jun 21 14:51:44 EDT 2007
I wonder if there is a (preferably not too-hackish) solution to the
following introspection problem: given a callable and a number of
positional and/or keyword arguments, infer what would be the frame's
locals() right after the function is called. For example, given:
def f(x, y=1, *a, **k):
z = x + y
w = len(a) - len (k)
return z * w
I'd like to have a function
def get_init_locals(callable, *args, **kwds):
# TODO
pass
so that:
>>> get_init_locals(f, 3)
{'a': (), 'k': {}, 'x': 3, 'y': 1}
>>> get_init_locals(f, 3, 4, 5)
{'a': (5,), 'k': {}, 'x': 3, 'y': 4}
>>> get_init_locals(f, 3, q=-1)
{'a': (), 'k': {'q': -1}, 'x': 3, 'y': 1}
Any takers ?
George
More information about the Python-list
mailing list