How to add my own “help” information to a python function/class?

て烟熏妆下的殇ゞ 提交于 2021-02-05 07:01:33

问题


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.

I wish to have my own package compiled, and others could see from "help()". How to do that?


回答1:


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"""

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'

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
[...]



回答2:


You could use argparse: https://docs.python.org/2/howto/argparse.html. It allows you to create a --help parameter that you can customize as well as add argument descriptions.

Example:

parser = argparse.ArgumentParser(description = "Write your help documentation here...")
parser.add_argument('config.txt', nargs='?', help='Write about your positional arguments here')
args = parser.parse_args()

So when someone runs your program with --help it will output:

$python yourProgram.py --help
usage: yourProgram.py [-h] [config.txt]

Write your help documentation here...

positional arguments:
config.txt  Write about your positional arguments here

optional arguments:
-h, --help  show this help message and exit


来源:https://stackoverflow.com/questions/40891380/how-to-add-my-own-help-information-to-a-python-function-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!