python中help函数_Python help()函数

python中help函数

Python help() function is used to get the documentation of specified module, class, function, variables etc. This method is generally used with python interpreter console to get details about python objects.

Python help()函数用于获取指定模块,类,函数,变量等的文档。此方法通常与python解释器控制台一起使用,以获取有关python对象的详细信息。

Python help()函数 (Python help() function)

Python help() function syntax is:

Python help()函数语法为:

help([object])

If no argument is given, the interactive help system starts on the interpreter console.

如果未提供任何参数,则交互式帮助系统将在解释器控制台上启动。

In python help console, we can specify module, class, function names to get their help documentation. Some of them are:

在python帮助控制台中,我们可以指定moduleclassfunction名称来获取其帮助文档。 他们之中有一些是:

help> True

help> collections

help> builtins

help> modules

help> keywords

help> symbols

help> topics

help> LOOPING

If you want to get out of help console, type quit.

如果要退出帮助控制台,请输入quit

We can also get the help documentation directly from the python console by passing a parameter to help() function.

通过将参数传递给help()函数,我们还可以直接从python控制台获取帮助文档。

>>> help('collections')

>>> help(print)

>>> help(globals)

Let’s see what is the output of help() function for globals() function.

让我们看看globals()函数的help()函数的输出是什么。

>>> help('builtins.globals')

Help on built-in function globals in builtins:

builtins.globals = globals()
    Return the dictionary containing the current scope's global variables.
    
    NOTE: Updates to this dictionary *will* affect name lookups in the current global scope and vice-versa.

定义自定义类和函数的help() (Defining help() for custom class and functions)

We can define help() function output for our custom classes and functions by defining docstring (documentation string). By default, the first comment string in the body of a method is used as its docstring. It’s surrounded by three double quotes.

我们可以通过定义docstring(文档字符串)来为自定义类和函数定义help()函数输出。 默认情况下,方法主体中的第一个注释字符串用作其文档字符串。 它用三个双引号引起来。

Let’s say we have a python file python_help_examples.py with following code.

假设我们有一个包含以下代码的python文件python_help_examples.py

def add(x, y):
    """
    This function adds the given integer arguments
    :param x: integer
    :param y: integer
    :return: integer
    """
    return x + y


class Employee:
    """
    Employee class, mapped to "employee" table in Database
    """
    id = 0
    name = ''

    def __init__(self, i, n):
        """
        Employee object constructor
        :param i: integer, must be positive
        :param n: string
        """
        self.id = i
        self.name = n

Notice that we have defined docstring for function, class and its methods. You should follow some format for documentation, I have generated some part of them automatically using PyCharm IDE. NumPy docstring guide is a good place to get some idea around proper way of help documentation.

注意,我们已经为函数,类及其方法定义了docstring。 您应该遵循某种格式的文档,我已经使用PyCharm IDE自动生成了其中的一部分。 NumPy文档字符串指南是了解正确的帮助文档方式的好地方。

Let’s see how to get this docstring as help documentation in python console.

让我们看看如何在python控制台中获取此文档字符串作为帮助文档。

First of all, we will have to execute this script in the console to load our function and class definition. We can do this using exec() command.

首先,我们将必须在控制台中执行此脚本以加载函数和类定义。 我们可以使用exec()命令执行此操作。

>>> exec(open("python_help_examples.py").read())

We can verify that the functions and class definitions are present using globals() command.

我们可以使用globals()命令验证函数和类定义是否存在。

>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__warningregistry__': {'version': 0}, 'add': <function add at 0x100dda1e0>, 'Employee': <class '__main__.Employee'>}

Notice that ‘Employee’ and ‘add’ are present in the global scope dictionary.

请注意,全局范围字典中存在“雇员”和“添加”。

Now we can get the help documentation using help() function. Let’s look at some of the examples.

现在,我们可以使用help()函数获取帮助文档。 让我们看一些例子。

>>> help('python_help_examples')
>>> help('python_help_examples.add')

Help on function add in python_help_examples:

python_help_examples.add = add(x, y)
    This function adds the given integer arguments
    :param x: integer
    :param y: integer
    :return: integer
(END)
>>> help('python_help_examples.Employee')
>>> help('python_help_examples.Employee.__init__')


Help on function __init__ in python_help_examples.Employee:

python_help_examples.Employee.__init__ = __init__(self, i, n)
    Employee object constructor
    :param i: integer, must be positive
    :param n: string
(END)

摘要 (Summary)

Python help() function is very helpful to get the details about modules, classes, and functions. It’s always best practice to define docstring for the custom classes and functions to explain their usage.

Python help()函数对于获取有关模块,类和函数的详细信息非常有帮助。 为自定义类和函数定义docstring始终是最佳实践,以解释其用法。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22892/python-help-function

python中help函数

  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值