Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python introspection: description of the parameters a function takes

Is there is a tool similar to dir() for modules that will tell me what parameters a given function takes? For instance, I would like to do something like dir(os.rename) and have it tell me what parameters are documented so that I can avoid checking the documentation online, and instead use only the Python scripting interface to do this.

like image 945
sholsapp Avatar asked Aug 02 '10 19:08

sholsapp


People also ask

How can I see the details of a function in Python?

Python help() function is used to get the documentation of specified module, class, function, variables etc. This method is generally used with python interpreter console to get details about python objects.

How do you define a function with a parameter in Python?

The four steps to defining a function in Python are the following: Use the keyword def to declare the function and follow this up with the function name. Add parameters to the function: they should be within the parentheses of the function. End your line with a colon.

Which function can be used for introspection?

One introspection function that's particularly useful in our development is the isinstance() function.


2 Answers

I realize that you're more interested in help(thing) or thing.__doc__, but if you're trying to do programmatic introspection (instead of human-readable documentation) to find out about calling a function, then you can use the inspect module, as discussed in this question.

like image 66
Josh Kelley Avatar answered Oct 17 '22 01:10

Josh Kelley


help(thing) pretty prints all the docstrings that are in the module, method, whatever ...

like image 33
nate c Avatar answered Oct 17 '22 02:10

nate c