问题描述
我知道我可以使用"help()"来查看软件包中的现有帮助信息.但是,在编写了自己的函数/类之后,如何启用帮助"以查看帮助文档?我知道评论"的第一行是 doc 属性,但这不是我想要的.
I know I can use "help()" to see existing help information from packages. But after I wrote my own function/class, how can I enable "help" to see help document? I know the first line of "comment" is a doc attribute, but that's not what I wanted.
我希望自己编译一个软件包,其他人可以从"help()"中看到.该怎么做?
I wish to have my own package compiled, and others could see from "help()". How to do that?
推荐答案
help()
完全基于 __ doc __
属性(以及函数参数的自省),因此确保您的模块,类和函数都具有文档字符串.
help()
is entirely based on __doc__
attributes (and introspection of function arguments), so make sure your module, your classes and your functions all have a docstring.
文档字符串不是 注释,它是顶部的裸字符串文字:
A docstring is not a comment, it is a bare string literal right at the top:
"""This is a module docstring, shown when you use help() on a module"""
class Foo:
"""Help for the class Foo"""
def bar(self):
"""Help for the bar method of Foo classes"""
def spam(f):
"""Help for the spam function"""
例如,流行的第三方 requests
模块具有文档字符串:
For example, the popular third-party requests
module has a docstring:
>>> import requests
>>> requests.__doc__
'\nRequests HTTP library\n~~~~~~~~~~~~~~~~~~~~~\n\nRequests is an HTTP library, written in Python, for human beings. Basic GET\nusage:\n\n >>> import requests\n >>> r = requests.get(\'https://www.python.org\')\n >>> r.status_code\n 200\n >>> \'Python is a programming language\' in r.content\n True\n\n... or POST:\n\n >>> payload = dict(key1=\'value1\', key2=\'value2\')\n >>> r = requests.post(\'http://httpbin.org/post\', data=payload)\n >>> print(r.text)\n {\n ...\n "form": {\n "key2": "value2",\n "key1": "value1"\n },\n ...\n }\n\nThe other HTTP methods are supported - see `requests.api`. Full documentation\nis at <http://python-requests.org>.\n\n:copyright: (c) 2016 by Kenneth Reitz.\n:license: Apache 2.0, see LICENSE for more details.\n'
由 help()
直接呈现的
以及模块内容(以及递归的内容文档字符串):
which is rendered by help()
directly, together with the module contents (and recursively, the content docstrings):
>>> help('requests')
Help on package requests:
NAME
requests
DESCRIPTION
Requests HTTP library
~~~~~~~~~~~~~~~~~~~~~
Requests is an HTTP library, written in Python, for human beings. Basic GET
usage:
>>> import requests
>>> r = requests.get('https://www.python.org')
>>> r.status_code
200
[...]
这篇关于如何添加我自己的“帮助"信息到python函数/类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!