Python对象的文档字符串(DocString):__doc__的写法和输出方法

Python有个特性叫做文档字符串,即DocString,这个特性可以让你的程序文档更加清晰易懂,在python的系统文件里,也都在使用这个特性。因此推荐每个Python爱好者都使用它。

       DocString是有自己的格式的。一般是放在自己函数定义的第一行,用‘’符号指示,在这‘’里面添加函数功能说明就可以了。这个说明可以使用.__doc__(注意前后都是双_)属性,将DocString特性print打印出来。如下:

[python]  view plain  copy
  1. def inputxy():  
  2.     '''''this is my first test doc'''  
  3.   
  4.     print "hello the world"  
  5. inputxy()  
  6. print inputxy.__doc__# print pirntXY's doc  

输出内容如下:

[python]  view plain  copy
  1. hello the world  
  2. this is my first test doc  
  3.   
  4. Process finished with exit code 0  

DocSting的典型用法就是help()调用,它抓取DocString属性,清晰的给你展示出来。

猜你喜欢

转载自blog.csdn.net/qq_37811638/article/details/79100447