我有一个模块,我正在尝试使用help函数调用文档字符串。下面是模块:
"""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)时,我得到了关于模块单词的帮助:
NAME
words
FUNCTIONS
fetch_words()
FILE
c:\users\cacheson\pyfund\words.py任何帮助都将不胜感激
发布于 2018-03-11 23:48:48
您只需要调用help,将函数名作为参数传递,或者调用模块名本身:
>>> 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正如你所看到的,它对我来说工作得很好,所以问题不在你的代码中。
https://stackoverflow.com/questions/49221817
复制相似问题