Is there an easy way to be inside a python function and get a list of the parameter names?
For example:
def func(a,b,c):
print magic_that_does_what_I_want()
>>> func()
['a','b','c']
Thanks
Is there an easy way to be inside a python function and get a list of the parameter names?
For example:
def func(a,b,c):
print magic_that_does_what_I_want()
>>> func()
['a','b','c']
Thanks
Well we don't actually need inspect
here.
>>> func = lambda x, y: (x, y)
>>>
>>> func.__code__.co_argcount
2
>>> func.__code__.co_varnames
('x', 'y')
>>>
>>> def func2(x,y=3):
... print(func2.__code__.co_varnames)
... pass # Other things
...
>>> func2(3,3)
('x', 'y')
>>>
>>> func2.__defaults__
(3,)
inspect
for Python 2/3 compatibility. –
Googolplex __code__
seems to be backported. func_code
also still works. –
Revell __code__
was introduced in Python 2.6, released in October 2008. 3.0 came out in December 2008. So I wouldn't say it was "backported" - 3.0 branched off of 2.6. The only version with features that were backported from 3 is 2.7. –
Mythological func.func_code.co_varnames[:func.func_code.co_argcount]
excludes any keyword-only arguments, i.e. for a function defined as def f(a, b, *, c, d):
this will only return ('a', 'b')
. –
Radioactivity locals()
returns a dictionary with local names:
def func(a, b, c):
print(locals().keys())
prints the list of parameters. If you use other local variables those will be included in this list. But you could make a copy at the beginning of your function.
print locals().keys()
will return ['arg']
. I used print locals.get('arg')
–
Boon ['a', 'b', 'c']
(possibly not in a-b-c order), as expected. Your solution (a) doesn't work, raises an AttributeError
-- maybe you meant print locals().get('arg')
? and (b) if that's what you were trying to do, that prints the value of the parameter, not the name of the parameter as the OP requested. –
Anglophobe "found {thing} in {place}, took {action}, resulting in {result}".format(**locals())
instead of "found {thing} in {place}, took {action}, resulting in {result}".format(thing=thing, place=place, action=action, result=result)
–
Oscillator locals()
returns namespace vars too, eg def func(a,b,c): d=4; print(locals()['d'])
–
Octavus f'found {thing} in {place}, took {action}, resulting in {result}'
–
Pegpega locals()
with vars()
. –
Parabolize locals()
will contain them all. –
Hager def func(a, b, c): args = locals(); d = 55; print(args); print(locals())
prints args
only with a
, b
,c
, but locals
printed at the end includes d
and also args
. So, making a copy at the beginning seems to work. –
Twocolor If you also want the values you can use the inspect
module
import inspect
def func(a, b, c):
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
print 'function name "%s"' % inspect.getframeinfo(frame)[2]
for i in args:
print " %s = %s" % (i, values[i])
return [(i, values[i]) for i in args]
>>> func(1, 2, 3)
function name "func"
a = 1
b = 2
c = 3
[('a', 1), ('b', 2), ('c', 3)]
def foo(first, second, third, *therest):
? –
Stirrup def decorate(fn): ...
, you would simply do fn_params = inspect.signature(fn).parameters
in the decorator and use logic to get the argument values in fn
's wrapper, def wrapper(*args, **kwargs): ...
, which is returned by the decorator. –
Wendell import inspect
def func(a,b,c=5):
pass
>>> inspect.getargspec(func) # inspect.signature(func) in Python 3
(['a', 'b', 'c'], None, None, (5,))
so for getting arguments list alone use:
>>> inspect.getargspec(func)[0]
['a', 'b', 'c']
inspect.getargspec(func)
from within func
should work just fine –
Lubricity inspect.signature
–
Meldameldoh © 2022 - 2024 — McMap. All rights reserved.
func.func_code.co_varnames[:func.func_code.co_argcount]
since co_varnames is a tuple of all variables present in the function – Iver