開發時加入註釋有助於描述思考過程,並幫助自己和其他人了解意圖,可以更輕鬆地發現錯誤、改進程式,以及在其他地方做更多應用。
單行註釋
加入註釋以 # 開頭,
# defining the start code
startCode = 50
也可加在程式碼後方,會被忽略,
startCode = 50 # defining the start code
注意不要加入無用的描述,
如同變數命名時不要取沒意義的名稱。
多行註釋
當要註釋的內容很多,或是撰寫文件、功能之類的,可以使用這種方式。
PEP8中建議單行不要超過79個字,一般情況則是會照公司或是團隊的開發習慣決定。
多行#開頭,
# PythonComments version 1.0.3
#
# -a (--all): show all features
# -h (--help): show the help
# .....
或是用""" 包住
"""
PythonComments version 1.0.3
-a (--all): show all features
-h (--help): show the help
.....
"""
發表留言