Python類和方法注釋規(guī)范說(shuō)明
Python類和方法注釋規(guī)范
注釋風(fēng)格
reStructuredText(PyCharm默認(rèn))
def func(path, field_storage, temporary): '''基本描述 詳細(xì)描述 :param path: The path of the file to wrap :type path: str :param field_storage: The :class:`FileStorage` instance to wrap :type field_storage: FileStorage :param temporary: Whether or not to delete the file when the File instance is destructed :type temporary: bool :returns: A buffered writable file descriptor :rtype: BufferedFileStorage ''' pass
def func(path, field_storage, temporary): '''基本描述 詳細(xì)描述 Parameters ---------- path : str The path of the file to wrap field_storage : FileStorage The :class:`FileStorage` instance to wrap temporary : bool Whether or not to delete the file when the File instance is destructed Returns ------- BufferedFileStorage A buffered writable file descriptor ''' pass
Google(官方推薦)
def func(path, field_storage, temporary): '''基本描述 詳細(xì)描述 Args: path (str): The path of the file to wrap field_storage (FileStorage): The :class:`FileStorage` instance to wrap temporary (bool): Whether or not to delete the file when the File instance is destructed Returns: BufferedFileStorage: A buffered writable file descriptor ''' pass
風(fēng)格 | 特點(diǎn) | 適用 |
---|---|---|
reStructuredText | 用冒號(hào)分隔 | PyCharm默認(rèn) |
NumPy | 用下劃線分隔 | 傾向垂直,長(zhǎng)而深的文檔 |
用縮進(jìn)分隔 | 傾向水平,短而簡(jiǎn)單的文檔 |
Sphinx對(duì)NumPy和Google風(fēng)格的對(duì)比,英文不好可以參考中文版
小技巧
在PyCharm中Ctrl+Q可快速查看注釋
代碼規(guī)范(含代碼注釋)
代碼縮進(jìn)和冒號(hào)
注意條件語(yǔ)句必須嚴(yán)格控制縮進(jìn),保證父句和子句的關(guān)系
num = 10 if num>5: ? ? print('yes') else: ? ? print('no')
空行分隔代碼段
例如if語(yǔ)句判斷、while循環(huán)、for循環(huán)、def函數(shù)、class類等代碼段前后最好留一行(人工分好段落)
# if語(yǔ)句 if num>5: ? ? print('yes') else: ? ? print('no') ? # for循環(huán) for i in (1,2,4): ? ? print(i) ? # while循環(huán) while i>3: ? ? print('yes') ? ? i+=1 else: ? ? print('end') ? ?? # 函數(shù)定義 def show(): ? ? print(132) ? # 類定義 class Person: ? ? def show(self): ? ? ? ? print(123)
包、模塊的命名規(guī)范
1. 包——要求統(tǒng)一用小寫(xiě)(相當(dāng)于文件夾)
2.模塊——要求統(tǒng)一用小寫(xiě)(相當(dāng)于文件夾里的文件)
類和對(duì)象的命名規(guī)范
1. 類——嚴(yán)格的駝峰式寫(xiě)法eg.IndexUserPerson
2. 對(duì)象——要求統(tǒng)一用小寫(xiě)
函數(shù)的命名規(guī)范
駝峰式寫(xiě)法 eg.indexUserPerson(不強(qiáng)行)
代碼注釋
1.單行注釋——#
2.多行注釋——(快捷鍵為Ctrl+/)
'''
三對(duì)單引號(hào),python多行注釋符'''
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python探索之實(shí)現(xiàn)一個(gè)簡(jiǎn)單的HTTP服務(wù)器
這篇文章主要介紹了Python探索之實(shí)現(xiàn)一個(gè)簡(jiǎn)單的HTTP服務(wù)器,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10pyinstaller打包django項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了pyinstaller打包django項(xiàng)目的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09python使用hdfs3模塊對(duì)hdfs進(jìn)行操作詳解
這篇文章主要介紹了python使用hdfs3模塊對(duì)hdfs進(jìn)行操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06使用py2exe在Windows下將Python程序轉(zhuǎn)為exe文件
這篇文章主要介紹了Windows下用py2exe將Python程序轉(zhuǎn)為exe文件的方法,注意py2exe只是負(fù)責(zé)文件格式的轉(zhuǎn)換,并不能將Python程序編譯為機(jī)器碼,要的朋友可以參考下2016-03-03python?sklearn與pandas實(shí)現(xiàn)缺失值數(shù)據(jù)預(yù)處理流程詳解
對(duì)于缺失值的處理,主要配合使用sklearn.impute中的SimpleImputer類、pandas、numpy。其中由于pandas對(duì)于數(shù)據(jù)探索、分析和探查的支持較為良好,因此圍繞pandas的缺失值處理較為常用2022-09-09Python 實(shí)現(xiàn)一個(gè)手機(jī)號(hào)碼獲取妹子名字的功能
這篇文章主要介紹了Python 實(shí)現(xiàn)一個(gè)手機(jī)號(hào)碼獲取妹子名字的功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09