python 的Docstrings 文档字符串

http://www.kuqin.com/abyteofpython_cn/ch07s07.html

使用 DocStrings


    #!/usr/bin/python
    # Filename: func_doc.py

    def printMax(x, y):
        '''Prints the maximum of two numbers.

        The two values must be integers.'''
        x = int(x) # convert to integers, if possible
        y = int(y)

        if x > y:
             print x, 'is maximum'
        else:
             print y, 'is maximum'

    printMax(3, 5)
    print printMax.__doc__

(源文件:模块一章学习更多关于对象的知识。

如果你已经在 Python 中使用过 help(),那么你已经看到过 DocStings 的使用了!它所做的只是抓取函数的doc属性,然后整洁地展示给你。你可以对上面这个函数尝试一下——只是在你的程序中包括 help(printMax)。记住按 q 退出 help。

自动化工具也可以以同样的方式从你的程序中提取文档。因此,我 强烈建议 你对你所写的任何正式函数编写文档字符串。随你的 Python 发行版附带的 pydoc 命令,与 help()类似地使用 DocStrings。


猜你喜欢

转载自blog.csdn.net/kllkzl3/article/details/79710217