python查看文档的软件_Python __doc__属性:查看文档
前面介紹了使用 help() 函數來查看程序單元的幫助信息。比如導入 string 模塊之后,即可使用 help() 函數來查看指定程序單元的幫助信息。
例如,在交互式解釋器中輸入如下命令來查看 string 模塊下 capwords() 函數的作用:
>>> help(string.capwords) Help on function capwords in module string: capwords(s, sep=None) capwords(s [,sep]) -> string Split the argument into words using split, capitalize each word using capitalize, and join the capitalized words using join. If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.
通過上面描述可以看到,capwords() 函數的作用就是將給定的 s 字符串中每個單詞的首字母變成大寫的。該函數可通過 sep 參數指定分隔符:如果不指定 sep 參數,該字符串默認以空白作為分隔符。
在查看了幫助信息之后,接下來通過如下命令來測試 string.capwords() 函數的用法:
>>>string.capwords('abc xyz')
'Abc Xyz'
>>> string.capwords('abc;xyz',sep=';')
'Abc;Xyz'
上面代碼在第一次使用 capwords() 函數時,沒有指定 sep 參數,因此默認以空格為分隔符,這意味著程序將 abc xyz 分成 abc 和 xyz 兩個單詞,因此該函數將 a、x 兩個字母變成大寫的:在第二次使用 capwords() 函數時,指定 sep 參數為“;”,這意味著以“,”為分隔符將 abc;xyz 分成 abc 和 xyz 兩個單詞,因此程序將 a、x 兩個字母變成大寫的。
需要說明的是,使用 help() 函數之所以能查看到程序單元的幫助信息,其實完全是因為該程序單元本身有文檔信息,也就是有 __doc__ 屬性。換句話說,使用 help() 函數查看的其實就是程序單元的 __doc__ 屬性值。
例如,使用 print(string.capwords.__doc__) 命令來查看 capwords() 的幫助信息,將會看到如下輸出結果:
>>> print(string.capwords.__doc__)
capwords(s [,sep]) -> string
Split the argument into words using split, capitalize each
word using capitalize, and join the capitalized words using
join.? If the optional second argument sep is absent or None,
runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise
sep is used to split and join the words
對比 help(string.capwords) 和 print(string.capwords.__doc__) 兩個命令的輸出結果,不難看到它們的輸出結果完全相同,這說明使用 help() 函數查看的就是程序單元的__doc__屬性值。
從理論上說,應該為每個程序單元都編寫完備而詳細的文檔信息,這樣開發者只要通過 help() 函數即可查看該程序單元的文檔信息,完全不需要查看文檔。但不得不說的是,有些程序單元的文檔信息并不是很詳細,此時可能需要借助于 Python 庫的參考文檔:https://docs.python.org/3/library/index.html.
總結
以上是生活随笔為你收集整理的python查看文档的软件_Python __doc__属性:查看文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的恐龙中哪些恐龙比较强?我的恐龙最强恐
- 下一篇: 疫情感染分布