R S R S - 17 days ago 5
Python Question

Getting list of parameter names inside python function


Possible Duplicate:

Getting method parameter names in python




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

Answer

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)]
Comments