2

Possible Duplicate:
Getting list of parameters inside python function

E.g. supposes I have

def foo(a, b='B'): return

How can I ask foo to tell me that it has required parameter 'a', and parameter 'b', which has 'B' as it's default value?

Community
  • 1
  • 1
allyourcode
  • 21,871
  • 18
  • 78
  • 106

1 Answers1

4

Use inspect.getargspec.

def foo(a, b='B'): pass

import inspect
print inspect.getargspec(foo)

It may appear to be unclear which argument the default is for, but since non-default arguments can't follow default arguments, the default has to be for the 2nd argument.

Edit: The linked duplicate is good, an answer there shows you can get the same info without inspect, using func.func_code.co_varnames and func.func_defaults or func.__defaults__.

agf
  • 171,228
  • 44
  • 289
  • 238
  • link + explanation of something that is very likely to be unclear + alternate solution = MEGA awesome answer!!! :D – allyourcode Sep 19 '11 at 19:27