2

Possible Duplicate:
How to find out the arity of a method in Python

Given a python function, how do I programmatically determine the number of parameters it takes?

Community
  • 1
  • 1
Neil G
  • 32,138
  • 39
  • 156
  • 257
  • 3
    possible duplicate : http://stackoverflow.com/questions/990016/how-to-find-out-the-arity-of-a-method-in-python, http://stackoverflow.com/questions/3913963/length-of-arguments-of-python-function/3915056#3915056 – mouad Jan 12 '11 at 22:52

3 Answers3

11

inspect is your friend in this case

>>> def testFunc(arg1, arg2, arg3, additional='test'):
...     print arg1
... 
>>> import inspect
>>> inspect.getargspec(testFunc)
ArgSpec(args=['arg1', 'arg2', 'arg3', 'additional'], varargs=None, keywords=None, defaults=('test',))
>>> 
sberry
  • 128,281
  • 18
  • 138
  • 165
4

From outside the function, you can use inspect.getargspec(): http://docs.python.org/library/inspect.html#inspect.getargspec

cezio
  • 654
  • 3
  • 11
2

Take a look at the inspect.getargspec(func) command. That gives you a tuple, the first element of which is a list of the required parameters.

chmullig
  • 13,006
  • 5
  • 35
  • 52