Inferring initial locals()

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Thu Jun 21 16:42:54 EDT 2007


On Jun 21, 8:51 pm, George Sakkis <george.sak... at gmail.com> wrote:
> 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}

You might be intersted in the 'inspect' module.





More information about the Python-list mailing list