博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python中help函数_Python help()函数
阅读量:2534 次
发布时间:2019-05-11

本文共 4709 字,大约阅读时间需要 15 分钟。

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 , , names to get their help documentation. Some of them are:

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

help> Truehelp> collectionshelp> builtinshelp> moduleshelp> keywordshelp> symbolshelp> topicshelp> 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 function.

让我们看看函数的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 + yclass 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. is a good place to get some idea around proper way of help documentation.

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

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__': 
, '__spec__': None, '__annotations__': {}, '__builtins__':
, '__warningregistry__': {'version': 0}, 'add':
, '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始终是最佳实践,以解释其用法。

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

python中help函数

转载地址:http://nxmzd.baihongyu.com/

你可能感兴趣的文章
生成二维码图片的工具类
查看>>
Surface Pro 4远程桌面分辨率问题
查看>>
【转】包管理器Bower详细讲解
查看>>
JS膏集02
查看>>
程序员三个境界
查看>>
从微信小程序开发者工具源码看实现原理(一)- - 小程序架构设计
查看>>
ASP.NET 上的 Async/Await 简介
查看>>
Effective C++学习笔记(1)
查看>>
element-ui 组件源码分析整理笔记目录
查看>>
GridEh排序
查看>>
[oc学习笔记]多态
查看>>
Tomcat connectionTimeout问题定位处理
查看>>
【PP系列】SAP 取消报工后修改日期
查看>>
ZooKeeper学习第四期---构建ZooKeeper应用(转)
查看>>
JNday4-am
查看>>
UI控件(复习一下)
查看>>
window下自己主动备份数据库成dmp格式的bat写法
查看>>
Memcache存储大数据的问题
查看>>
HDU 5050 Divided Land(进制转换)
查看>>
python进阶学习笔记(三)
查看>>