Python中print(__doc__)作用
阿新 • • 發佈:2018-12-01
作用
輸出檔案開頭註釋的內容
具體用法
"""This is the module docstring."""
def f(x):
"""This is the function docstring."""
return 2 * x
2)執行
>>> import mymodule
>>> mymodule.__doc__
'This is the module docstring.'
>>> mymodule.f.__doc__
'This is the function docstring.'
相關知識
Python有個特性叫做文件字串,即DocString,這個特性可以讓你的程式文件更加清晰易懂,在python的系統檔案裡,也都在使用這個特性。因此推薦每個Python愛好者都使用它。
DocString是有自己的格式的。一般是放在自己函式定義的第一行,用‘’符號指示,在這‘’裡面新增函式功能說明就可以了。這個說明可以使用.doc(注意前後都是雙_)屬性,將DocString特性print打印出來。
DocSting的典型用法就是help()呼叫,它抓取DocString屬性,清晰的給你展示出來。