[英]Calling docstrings from module
我有一个模块,我正在尝试使用帮助功能调用文档字符串。 这是模块:
"""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(words)时,我会获得有关模块单词的帮助:
NAME
words
FUNCTIONS
fetch_words()
FILE
c:\users\cacheson\pyfund\words.py
任何帮助将不胜感激
您只需要通过传递函数名称作为参数或模块名称本身来调用帮助:
>>> 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
如您所见,它对我来说很好用,因此问题不在您的代码中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.