How do I print out which arguments a Python function requires/allows?
Suppose 开发者_Go百科I have a function and I want to print out the arguments it accepts. How can I do this?
Use inspect.getargspec()
to find out.
I see that someone has already offered the answer i had in mind, so i'll suggest a purely practical one. IDLE will give you a function's parameters as a 'tooltip'.
This should be enabled by default; the tooltip will appear just after you type the function name and the left parenthesis.
To do this, IDLE just accesses the function's doc string, so it will show the tooltip for any python function--standard library, third-party library, or even a function you've created earlier and is in a namespace accessible to IDLE.
Obviously, this is works only when you are working in interactive mode in IDLE, though it does have the advantage of not requiring an additional function call.
The help
function does this.
All you have to do is put in docstrings for your functions.
if you use IPython (as you absolutely should), use
foo?
to see the documentation, including what the function expects, and:
foo??
to see the above documentation plus the source code (if available)
精彩评论