R S R S - 1 year ago 126
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()


Answer Source

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)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download