getlocals and getargs

Lee leeyuan123 at gmail.com
Wed Oct 28 01:43:13 EDT 2009


Yes, It's actually quite easy to implement

getlocals = lambda fn: fn.func_code.co_varnames[:]
getargs = lambda fn: getlocals()[:fn.func_code.co_argcount]

Of course, this doesn't take into account *args and **kwargs. To
figure out if a function have args and kwargs, do a binary and (&)
between func_code.co_flags and either 4 (*args) or 8 (**kwargs). The
indices of the two local variables are co_argcount and co_argcount+1
(so the entire expression for *args would just be if
fn.func_code.co_flags&4: fn.func_code.co_varnames[argcount])



More information about the Python-list mailing list