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帮助控制台中,我们可以指定module , class , function名称来获取其帮助文档。 他们之中有一些是:

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 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 + yclass Employee:"""Employee class, mapped to "employee" table in Database"""id = 0name = ''def __init__(self, i, n):"""Employee object constructor:param i: integer, must be positive:param n: string"""self.id = iself.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函数

python中help函数_Python help()函数相关推荐

  1. python中dir用法_Python dir() 函数

    Python dir() 函数 描述 dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__(),该方法将被调用.如 ...

  2. python中dir用法_Python dir()函数

    您可以使用内置的dir()函数列出一个定义对象的标识符.例如,对于一个模块,包括在模块中定义的函数,类和变量. 当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表.当没有为其提供参 ...

  3. python中eps参数_Python minimize函数:向约束字典传递附加参数

    我不知道如何通过minimize函数将其他参数传递给约束字典.我可以成功地将其他参数传递给目标函数. constraints参数是一个dict,它有一个字段"args",其中arg ...

  4. python中factor函数_Python入门-函数

    函数 在维基百科上函数式这样描述的: 函数在数学中为两集合间的一种对应关系:输入值集合中的每项元素皆能对应唯一一项输出值集合中的元素. 此处的函数区别于我们数学上的函数,在编程世界中,函数(Funct ...

  5. python中实现延时回调普通函数示例代码

    这篇文章主要给大家介绍了关于python中实现延时回调普通函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧. 回调函 ...

  6. Python中常用的高阶函数

    Python 中常用的高阶函数 ① filter(function,iterable)filter(function, iterable)filter(function,iterable)   过滤器 ...

  7. Python中的startswith和endswith函数使用实例

    Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...

  8. python整数转换字符串_使用Python中的str()函数将整数值转换为字符串

    python整数转换字符串 Given an integer value and we have to convert the value to the string using str() func ...

  9. python中zip的使用_浅谈Python中的zip()与*zip()函数详解

    前言 1.实验环境: Python 3.6: 2.示例代码地址:下载示例: 3.本文中元素是指列表.元组.字典等集合类数据类型中的下一级项目(可能是单个元素或嵌套列表). zip(*iterables ...

  10. Python中常用最神秘的函数! lambda 函数深度总结!

    今天我们来学习 Python 中的 lambda 函数,并探讨使用它的优点和局限性 Let's do it! 什么是 Python 中的 Lambda 函数 lambda 函数是一个匿名函数(即,没有 ...

最新文章

  1. 刷题:二叉树的遍历方式及根据遍历结果还原二叉树
  2. ux设计_从UX设计人员的角度来看Microsoft Build 2018
  3. flutter已经支持安卓热更新_flutter 在 android 上的热更新
  4. 时光机穿梭---管理修改
  5. Sqoop1 From PostgreSQL to Hdfs
  6. DevExpress DXperience最新版订阅加载程序
  7. c++ 两个栈实现一个队列
  8. 计算机电子智能化贰级,电子与智能化工程专业承包资质分为一级、二级。
  9. IntelliJ IDEA 记学习笔《Patch 创建补丁》
  10. floor关键字用法:
  11. 计算机毕业设计SSM超市商品管理系统【附源码数据库】
  12. Js上传图片到七牛云(直接上代码)
  13. 触发onclick事件元素的获取
  14. JSAAS开源社区版
  15. 深圳Python学习:Python几大问,你想知道的答案都在这里!-千锋
  16. labelimg闪退解决方法(多人检测有效,根本原因)
  17. 计量经济学 (1.2)
  18. visual studio控制台中文乱码问题
  19. 精简 Python 发展史
  20. Apache开源列式存储引擎Parquet和ORC比较

热门文章

  1. net use 使用
  2. [转载] python异常和错误有什么区别_python的错误和异常
  3. [转载] python 判断字符串是否包含另一个字符串_强烈推荐:Python字符串(string)方法整理(一)...
  4. [转载] 使用Python处理Excel文件
  5. [转载] python跨行 print:多用(),换行符\要小心,少用+或者不用(其它程序代码跨行用\就行,不能用括号)
  6. CF1041E Tree Reconstruction_构造_思维题
  7. mysql 联合索引匹配原则
  8. Salesforce删除数据时出现Insufficient privileges的可能原因
  9. delphi 判断两个时间差是否在一个指定范围内
  10. 解决WEB页面上焦点控制一法