从模块调用docstrings

时间:2018-03-11 15:41:30

标签: python docstring

我有一个模块,我试图使用帮助功能调用文档字符串。这是模块:

"""Retrieve and print words from a URL.

Usage:

    py words.py <URL>
"""
import sys
from urllib.request import urlopen

def fetch_words(url):
    """Fetch a list of words from a URL.

    Args:
        url: The URL of a UTF-8 text docuemnt.

    Returns:
        A list of strings containing the words from the document.
    """
    with urlopen(url) as story:
        story_words = []
        for line in story:
            line_words = line.decode('utf-8').split()
            for word in line_words:
            story_words.append(word)
    return story_words


def print_items(items):
    """Print items one per line.

    Args:
        url: The URL of a UTF-8 text.
    """
    for item in items:
        print(item)


def main(url):
    """Print each words form a text document from a URL.

    Args:
        url: The URL of a UTF -8 text document.
    """
    words = fetch_words(url)
    print_items(words)


if __name__ == '__main__':
    main(sys.argv[1])

当我输入命令帮助(单词)时,我得到模块单词的帮助:

NAME
    words

FUNCTIONS
    fetch_words()

FILE
    c:\users\cacheson\pyfund\words.py

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

你只需要调用帮助传递函数的名称作为参数,或者模块的名称本身:

>>> import words
>>> help(words)
Help on module words:

NAME
    words - Retrieve and print words from a URL.

DESCRIPTION
    Usage:

        py words.py <URL>

FUNCTIONS
    fetch_words(url)
        Fetch a list of words from a URL.

        Args:
            url: The URL of a UTF-8 text docuemnt.

        Returns:
            A list of strings containing the words from the document.

    print_items(items)
        Print items one per line.

        Args:
            url: The URL of a UTF-8 text.

FILE
    /home/mikel/Documents/stackoverflow/docstring/words.py

您可以看到它对我来说很好,所以问题不在您的代码中。