2

有没有一种方法可以检查函数或方法在 python 内部的作用,类似于 Matlab 中的帮助函数。我想获得函数的定义而不必谷歌它。

4

2 回答 2

10

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

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

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

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

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

于 2013-04-25T22:28:30.693 回答
1

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

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

于 2013-04-25T22:41:07.947 回答