Python有个特性叫做文档字符串,即DocString,这个特性可以让你的程序文档更加清晰易懂,在python的系统文件里,也都在使用这个特性。因此推荐每个Python爱好者都使用它。
DocString是有自己的格式的。一般是放在自己函数定义的第一行,用‘’符号指示,在这‘’里面添加函数功能说明就可以了。这个说明可以使用.__doc__(注意前后都是双_)属性,将DocString特性print打印出来。如下:
- def inputxy():
- '''''this is my first test doc'''
- print "hello the world"
- inputxy()
- print inputxy.__doc__# print pirntXY's doc
输出内容如下:
- hello the world
- this is my first test doc
- Process finished with exit code 0
DocSting的典型用法就是help()调用,它抓取DocString属性,清晰的给你展示出来。