I want to define a function using explicit argument names ff(a,b,c) in the function definition, but I also want to map a function over all arguments to get a list:
ff(a,b,c):
return list(map(myfunc,[a,b,c]))
However, I don't want to explicitly write parameter names inside function as a,b,c. I want to do it like
ff(a,b,c):
return list(map(myfunc,getArgValueList()))
getArgValueList() will retrieve the argument values in order and form a list. How to do this? Is there a built-in function like getArgValueList()?
What you're trying to do is impossible without ugly hacks. You either take
*argsand get a sequence of parameter values that you can use asargs:… or you take three explicit parameters and use them by name:
… but it's one or the other, not both.
Of course you can put those values in a sequence yourself if you want:
… but I'm not sure what that buys you.
If you really want to know how to write a
getArgValueListfunction anyway, I'll explain how to do it. However, if you're looking to make your code more readable, more efficient, more idiomatic, easier to understand, more concise, or almost anything else, it will have the exact opposite effect. The only reason I could imagine doing something like this is if you had to generate functions dynamically or something—and even then, I can't think of a reason you couldn't just use*args. But, if you insist:If you want to know how it works, most of it's in the
inspectmodule docs:currentframe()gets the current frame—the frame ofgetArgValueList.f_backgets the parent frame—the frame of whoever calledgetArgValueList.f_codegets the code object compiled from the function body of whoever calledgetArgValueList.co_varnamesis a list of all local variables in that body, starting with the parameters.co_argcountis a count of explicit positional-or-keyword parameters.f_localsis a dict with a copy of thelocals()environment of the frame.This of course only works for a function that takes no
*args, keyword-only args, or**kwargs, but you can extend it to work for them as well with a bit of work. (Seeco_kwonlyargcount,co_flags,CO_VARARGS, andCO_VARKEYWORDSfor details.)Also, this only works for CPython, not most other interpreters. and it could break in some future version, because it's pretty blatantly relying on implementation details of the interpreter.