简体   繁体   中英

Python: Function Documentation

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM