Python代码规范(针对3.1.2版本) 1.以4个空格作为缩进层次 2.不要混用制表符和空格作为缩进层次 3.每一行代码字符数不要超过79,否则积极使用行连接符'/' 4.使用UTF-8编码 关于import Yes: import os import sys No: import sys, os import顺序如下: 1. standard library imports 2. related third party imports 3. local application/library specific imports 关于表达式和语句中空格 Yes: spam(ham[1], {eggs: 2}) No: spam( ham[ 1 ], { eggs: 2 } ) Yes: if x == 4: print(x, y); (x, y) = (y, x) No: if x == 4 : print(x , y); (x , y) = (y , x) Yes: spam(1) No: spam (1) Yes: dict['key'] = list[index] No: dict ['key'] = list [index] Yes: x = 1 y = 2 long_variable = 3 No: x = 1 y = 2 long_variable = 3 其它推荐的规范 Yes: i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b) No: i=i+1 submitted +=1 x = x*2 - 1 hypot2 = x*x + y*y c = (a+b) * (a-b) Yes: def complex(real, imag=0.0): return magic(r=real, i=image) No: def complex(real, image = 0.0): return magic(r = real, i = imag) Yes: if foo = 'blah': do_blah_thing() do_one() do_two() do_three() Rather not: if foo == 'blah': do_blah_thing() do_one(); do_two(); do_three() 注释(略) 命名规范 包和模块全用小写,简短精炼 类名大写字母开头(同异常名)