1. 程式人生 > >【python coding 6:文件字串】Python的文件字串DocStrings

【python coding 6:文件字串】Python的文件字串DocStrings

 Python有一個很奇妙的特性,稱為 文件字串 ,它通常被簡稱為 docstrings 。DocStrings是一個重要的工具,由於它幫助你的程式文件更加簡單易懂。

 其作用如同我們寫程式碼時所新增的註釋一樣,但是不同之處是這個註釋,可以使用print方法將其打印出來,具體例項見示例:

#!/bin/env python
'''2014-08-09 DJY<span style="font-size:18px;">'</span>''

#在定義的類名方法名的冒號之後,新增’’’XXXXXXXX’’’,作為對此程式碼塊的描述。
#在後面列印的類或者方法的時候,就會輸出該段描述。
class TestClass:
        '''This is TestClass' DocStrings'''

def func1():
        '''this is func1's DocStrings'''

def func2():
        '''this is func2'''

print 'func1 output: '+func1.__doc__
print 'func2 output: '+func2.__doc__
print 'TestClass output:'+TestClass.__doc__

執行結果: