Python:功能文档

时间:2013-04-25 22:27:40

标签: python

有没有办法检查python本身里面的函数或方法是什么,类似于Matlab中的help函数。我希望得到函数的定义,而不必谷歌。

2 个答案:

答案 0 :(得分:10)

是的,您可以在python交互式解释器中调用help(whatever)

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

您甚至可以传递(引用)关键字并获得非常详细的帮助:

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

甚至是一般主题:

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

  

调用内置帮助系统。 (此函数用于交互式使用。)如果未给出参数,则交互式帮助系统将在解释器控制台上启动。

     

如果参数是字符串,则查找字符串作为模块,函数,类,方法,关键字或文档主题的名称,并在控制台上打印帮助页面。

     

如果参数是任何其他类型的对象,则会生成对象的帮助页面。

答案 1 :(得分:1)

help()函数几乎可以为您提供帮助,但如果您搜索某些内容(比如要使用的模块),请键入help('modules'),它将搜索可用的模块。

然后,如果您需要查找有关模块的信息,请加载它并键入dir(module_name)以查看模块中定义的方法。