Python類和方法注釋規(guī)范說明
Python類和方法注釋規(guī)范
注釋風格
def func(path, field_storage, temporary): '''基本描述 詳細描述 :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): '''基本描述 詳細描述 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): '''基本描述 詳細描述 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
風格 | 特點 | 適用 |
---|---|---|
reStructuredText | 用冒號分隔 | PyCharm默認 |
NumPy | 用下劃線分隔 | 傾向垂直,長而深的文檔 |
用縮進分隔 | 傾向水平,短而簡單的文檔 |
Sphinx對NumPy和Google風格的對比,英文不好可以參考中文版
小技巧
在PyCharm中Ctrl+Q可快速查看注釋
代碼規(guī)范(含代碼注釋)
代碼縮進和冒號
注意條件語句必須嚴格控制縮進,保證父句和子句的關(guān)系
num = 10 if num>5: ? ? print('yes') else: ? ? print('no')
空行分隔代碼段
例如if語句判斷、while循環(huán)、for循環(huán)、def函數(shù)、class類等代碼段前后最好留一行(人工分好段落)
# if語句 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)一用小寫(相當于文件夾)
2.模塊——要求統(tǒng)一用小寫(相當于文件夾里的文件)
類和對象的命名規(guī)范
1. 類——嚴格的駝峰式寫法eg.IndexUserPerson
2. 對象——要求統(tǒng)一用小寫
函數(shù)的命名規(guī)范
駝峰式寫法 eg.indexUserPerson(不強行)
代碼注釋
1.單行注釋——#
2.多行注釋——(快捷鍵為Ctrl+/)
'''
三對單引號,python多行注釋符'''
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python探索之實現(xiàn)一個簡單的HTTP服務(wù)器
這篇文章主要介紹了Python探索之實現(xiàn)一個簡單的HTTP服務(wù)器,具有一定參考價值,需要的朋友可以了解下。2017-10-10pyinstaller打包django項目的實現(xiàn)步驟
本文主要介紹了pyinstaller打包django項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09使用py2exe在Windows下將Python程序轉(zhuǎn)為exe文件
這篇文章主要介紹了Windows下用py2exe將Python程序轉(zhuǎn)為exe文件的方法,注意py2exe只是負責文件格式的轉(zhuǎn)換,并不能將Python程序編譯為機器碼,要的朋友可以參考下2016-03-03python?sklearn與pandas實現(xiàn)缺失值數(shù)據(jù)預(yù)處理流程詳解
對于缺失值的處理,主要配合使用sklearn.impute中的SimpleImputer類、pandas、numpy。其中由于pandas對于數(shù)據(jù)探索、分析和探查的支持較為良好,因此圍繞pandas的缺失值處理較為常用2022-09-09Python 實現(xiàn)一個手機號碼獲取妹子名字的功能
這篇文章主要介紹了Python 實現(xiàn)一個手機號碼獲取妹子名字的功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09