Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function parameter count [duplicate]

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?

like image 777
Neil G Avatar asked Feb 21 '26 06:02

Neil G


2 Answers

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',))
>>> 
like image 172
sberry Avatar answered Feb 24 '26 15:02

sberry


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

like image 20
cezio Avatar answered Feb 24 '26 15:02

cezio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!