首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从模块调用docstring

从模块调用docstring
EN

Stack Overflow用户
提问于 2018-03-11 23:41:30
回答 1查看 115关注 0票数 1

我有一个模块,我正在尝试使用help函数调用文档字符串。下面是模块:

代码语言:javascript
复制
"""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])

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

代码语言:javascript
复制
NAME
    words

FUNCTIONS
    fetch_words()

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

任何帮助都将不胜感激

EN

回答 1

Stack Overflow用户

发布于 2018-03-11 23:48:48

您只需要调用help,将函数名作为参数传递,或者调用模块名本身:

代码语言:javascript
复制
>>> 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

正如你所看到的,它对我来说工作得很好,所以问题不在你的代码中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49221817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档