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?

2
  • You can see it with help(foo) Commented Sep 17, 2011 at 2:29
  • @JBernardo That works for humans, but how can my programs access that information? Presumably, help uses the mechanism that I'm looking for. Commented Sep 19, 2011 at 19:20

1 Answer 1

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__.

Sign up to request clarification or add additional context in comments.

1 Comment

link + explanation of something that is very likely to be unclear + alternate solution = MEGA awesome answer!!! :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.