国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python 常用 PEP8 編碼規范詳解

2020-02-23 04:18:45
字體:
來源:轉載
供稿:網友

Python 常用 PEP8 編碼規范

代碼布局

縮進

每級縮進用4個空格。 括號中使用垂直隱式縮進或使用懸掛縮進。

EXAMPLE:

# (垂直隱式縮進)對準左括號foo = long_function_name(var_one, var_two,    var_three, var_four)# (懸掛縮進) 一般情況只需多一層縮進foo = long_function_name( var_one, var_two, var_three, var_four)# (懸掛縮進) 但下面情況, 需再加多一層縮進, 和后續的語句塊區分開來def long_function_name( var_one, var_two, var_three, var_four): print(var_one)# 右括號回退my_list = [ 1, 2, 3, 4, 5, 6,]result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f',)

錯誤示范:

# 不使用垂直對齊時,第一行不能有參數。foo = long_function_name(var_one, var_two, var_three, var_four)# 參數的懸掛縮進和后續代碼塊縮進不能區別。def long_function_name( var_one, var_two, var_three, var_four): print(var_one)# 右括號不回退,不推薦my_list = [ 1, 2, 3, 4, 5, 6, ]result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', )

最大行寬

    每行最大行寬不超過 79 個字符 一般續行可使用反斜杠 括號內續行不需要使用反斜杠

EXAMPLE:

# 無括號續行, 利用反斜杠with open('/path/to/some/file/you/want/to/read') as file_1, / open('/path/to/some/file/being/written', 'w') as file_2: file_2.write(file_1.read())# 括號內續行, 盡量在運算符后再續行class Rectangle(Blob): def __init__(self, width, height,   color='black', emphasis=None, highlight=0): if (width == 0 and height == 0 and  color == 'red' and emphasis == 'strong' or  highlight > 100):  raise ValueError("sorry, you lose") if width == 0 and height == 0 and (color == 'red' or      emphasis is None):  raise ValueError("I don't think so -- values are %s, %s" %    (width, height))

空行

    兩行空行用于分割頂層函數和類的定義 單個空行用于分割類定義中的方法

EXAMPLE:

# 類的方法定義用單個空行分割,兩行空行分割頂層函數和類的定義。class A(object): def method1(): pass def method2(): passdef method3(): pass

模塊導入

    導入的每個模塊應該單獨成行 導入順序如下: (各模塊類型導入之間要有空行分割,各組里面的模塊的順序按模塊首字母自上而下升序排列)

    標準庫 相關的第三方庫 本地庫

EXAMPLE:

# 按模塊首字母排序導入, 依此遞推import activeimport adidasimport create

錯誤示例:

# 一行導入多模塊import sys, os, knife# 不按首字母導入import createimport activeimport beyond

字符串

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 来宾市| 洱源县| 哈巴河县| 天全县| 长白| 通山县| 云霄县| 抚顺县| 德安县| 杭州市| 开平市| 玉龙| 珲春市| 营山县| 滦平县| 玉门市| 聂荣县| 都兰县| 贵州省| 宝丰县| 松滋市| 罗山县| 松江区| 色达县| 登封市| 松阳县| 虞城县| 山丹县| 安康市| 兴化市| 曲沃县| 南康市| 信宜市| 海林市| 集安市| 巍山| 高碑店市| 永靖县| 哈尔滨市| 武平县| 绍兴市|