Python類和方法注釋規(guī)范說明
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
'''
passdef 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
'''
passGoogle(官方推薦)
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án)格控制縮進(jìn),保證父句和子句的關(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)一用小寫(相當(dāng)于文件夾)
2.模塊——要求統(tǒng)一用小寫(相當(dāng)于文件夾里的文件)
類和對(duì)象的命名規(guī)范
1. 類——嚴(yán)格的駝峰式寫法eg.IndexUserPerson
2. 對(duì)象——要求統(tǒng)一用小寫
函數(shù)的命名規(guī)范
駝峰式寫法 eg.indexUserPerson(不強(qiáng)行)
代碼注釋
1.單行注釋——#
2.多行注釋——(快捷鍵為Ctrl+/)
'''
三對(duì)單引號(hào),python多行注釋符'''
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中try excpet BaseException(異常處理捕獲)的使用
本文主要介紹了Python中try excpet BaseException(異常處理捕獲)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python實(shí)現(xiàn)網(wǎng)頁文件轉(zhuǎn)PDF文件和PNG圖片的示例代碼
這篇文章主要介紹了如何利用Python分別實(shí)現(xiàn)網(wǎng)頁文件轉(zhuǎn)為PDF文件和網(wǎng)頁文件轉(zhuǎn)PNG圖片的示例代碼,文中的代碼簡(jiǎn)潔易懂,感興趣的可以動(dòng)手試試2022-01-01
Django框架實(shí)現(xiàn)在線考試系統(tǒng)的示例代碼
這篇文章主要介紹了Django框架實(shí)現(xiàn)在線考試系統(tǒng)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python?Fire中兩種命令行參數(shù)靈活設(shè)置方式詳解
Python的Fire庫,一個(gè)用來生成命令行工具的的庫,這篇文章主要針對(duì)命令行參數(shù),補(bǔ)充兩種更加靈活的設(shè)置方式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
python 利用百度API識(shí)別圖片文字(多線程版)
這篇文章主要介紹了python 利用百度API識(shí)別圖片文字(多線程版),幫助大家更好的利用python進(jìn)行機(jī)器識(shí)別,感興趣的朋友可以了解下2020-12-12

