2

Is there a way to check what a function or a method does inside of python itself similar to the help function in Matlab. I want to get the definition of the function without having to Google it.

2 Answers 2

10

Yes, you can call help(whatever) inside python interactive interpreter.

>>> help
Type help() for interactive help, or help(object) for help about object.

>>> help(zip)
Help on built-in function zip in module __builtin__:

zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.

You can even pass (quoted) keywords and get very detailed help:

>>> help('if')
The ``if`` statement
********************

The ``if`` statement is used for conditional execution:

   if_stmt ::= "if" expression ":" suite
               ( "elif" expression ":" suite )*
               ["else" ":" suite]

It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section *Boolean operations*
for the definition of true and false); then that suite is executed
(and no other part of the ``if`` statement is executed or evaluated).
If all expressions are false, the suite of the ``else`` clause, if
present, is executed.

Related help topics: TRUTHVALUE

>>> help('def')
Function definitions
********************

A function definition defines a user-defined function object
....

Or even general topics:

>>> help('FUNCTIONS')
Functions
*********

Function objects are created by function definitions.  The only
operation on a function object is to call it: ``func(argument-list)``.

There are really two flavors of function objects: built-in functions
and user-defined functions.  Both support the same operation (to call
the function), but the implementation is different, hence the
different object types.

See *Function definitions* for more information.

Related help topics: def, TYPES

Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the interactive help system starts on the interpreter console.

If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console.

If the argument is any other kind of object, a help page on the object is generated.

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

1 Comment

Thanks for the quick and thorough answer. The string thing was pretty helpful, not sure why I haven't tried it exactly. Also after playing around I found that you have to type help('module.method') to get the help for the methods, so for sqrt help('math.sqrt') and for append it's help('list.append')
1

The help() function gives you help on almost everything but if your searching for something (like a module to use) then type help('modules') and it will search for available modules.

Then if you need to find information about a module load it and type dir(module_name) to see the methods that are defined in the module.

1 Comment

It is kind of amazing that I am through 150 pages of a python book and it has never mentioned dir() or help(). Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.