Python help() 函数用法

2023-09-17 22:25:55

Python help() 函数调用交互式内置帮助系统。如果参数是string,则字符串被视为modulefunctionclasskeyword或文档主题的名称,并在控制台上打印一个帮助页面。 如果参数是任何其他类型的对象,则会显示该对象的帮助页。

语法:

help(object)

参数:

对象:(可选)需要在控制台上打印其文档的对象。

return Vype:

返回帮助页。

下面显示了Python interactive shellbuiltin print method上的帮助。

>>> help('print')
Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

上面,指定的字符串与内置的print函数匹配,因此它会显示帮助。

下面显示了module上的帮助页面。

>>> help('math')
Help on built-in module math:

NAME
math

DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.

FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of x.

acosh(x, /)
Return the inverse hyperbolic cosine of x.

asin(x, /)
Return the arc sine (measured in radians) of x.

asinh(x, /)
Return the inverse hyperbolic sine of x.

atan(x, /)
Return the arc tangent (measured in radians) of x.
-- More --

上面,指定的stringMath模块匹配,因此它显示了 Math 函数的帮助。 -- More --意味着通过按Enterspace键可以显示更多信息。

交互式帮助(Interactive Help)

如果未给出参数,交互式帮助系统将在解释器控制台上启动,您可以在其中编写任何函数module名称以获取帮助,如下所示。

>>> help()
Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help>

现在,您可以编写任何内容来获取帮助。例如,编写打印以获取有关打印功能的帮助。

help> print
Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

help>

课程帮助(Help on Classes)

帮助功能还可用于内置或用户定义的classes和函数。

>>> help(int)

Help on class int in module builtins:

class int(object)
| int([x]) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). For floating point
| numbers, this truncates towards zero.
|
| If x is not a number or if base is given, then x must be a string,
| bytes, or bytearray instance representing an integer literal in the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
| >>> int('0b100', base=0)
| 4
|
| Methods defined here:
|
| __abs__(self, /)
| abs(self)
--More--

下面显示了用户自定义classdocstring

class student: 
    def __init__(self): 
        '''The student class is initialized'''
  
    def print_student(self): 
        '''Returns the student description'''
        print('student description') 
help(student)

输出:

Help on class student in module __main__:
class student(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(self)
 |      The student class is initialized
 |  
 |  print_student(self)
 |      Returns the student description
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

本文内容总结: