企业做网站好吗,wordpress go链接不跳转,四川公共资源交易信息网,免费咨询医生在哪里咨询基本格式 缩进 使用4个空格进行缩进 行宽 每行代码尽量不超过80个字符 理由#xff1a; 这在查看side-by-side的diff时很有帮助方便在控制台下查看代码太长可能是设计有缺陷换行 Python支持括号内的换行。这时有两种情况。 第二行缩进到括号的起始处foo long_function_name(v…基本格式 缩进 使用4个空格进行缩进 行宽 每行代码尽量不超过80个字符 理由 这在查看side-by-side的diff时很有帮助方便在控制台下查看代码太长可能是设计有缺陷换行 Python支持括号内的换行。这时有两种情况。 第二行缩进到括号的起始处 foo long_function_name(var_one, var_two,var_three, var_four) 第二行缩进4个空格适用于起始括号就换行的情形 def long_function_name(var_one, var_two, var_three,var_four): print(var_one) 使用反斜杠\换行二元运算符 .等应出现在行首并与上一行的.或对齐或者缩进4个空格。长字符串也可以用此法换行 foo variable_with_long_name \ another_variable \ variablesession.query(MyTable) \.filter_by(id1) \ .one() this_is_a_very_long(function_call, with many parameters) \ .that_returns_an_object_with_an_attribute print Hello, \ %s %s! % \ (Harry, Potter) 多个元素的list或者tuple在起始括号后换行第二行缩进4个空格 items [this is the first, set of items, with more items, to come in this line, like this ] 禁止复合语句即一行中包含多个语句 # yes
do_first()
do_second()
do_third()# no
do_first();do_second();do_third(); if/for/while一定要换行 # yes
if foo blah: do_blah_thing() # no if foo blah: do_blash_thing() 空行 模块级函数和类定义之间空两行类成员函数之间空一行不要使用太多的连续空行来区分代码的逻辑块 class A:This is a simple docstring.def __init__(self): pass def hello(self): pass def hello(name): print Hello %s! % name def main(): pass 可以使用多个空行分隔多组相关的函数函数中可以使用空行分隔出逻辑相关的代码表达式 空格 一元运算符不加空格在二元运算符两边各空一格[,-,,,,in,is not, and]: # yes
exp -1.05
i i 1 submitted 1 x x * 2 - 1 hypot2 x * x y * y c (a b) * (a - b) # no exp - 1.05 ii1 submitted 1 x x*2 - 1 hypot2 x*x y*y c (ab) * (a-b) 函数的参数列表中,之后要有空格 # yes
def complex(real, imag): pass # no def complex(real,imag): pass 函数的参数列表中默认值等号两边不要添加空格 # yes
def complex(real, imag0.0): pass # no def complex(real, imag 0.0): pass 左括号之后右括号之前不要加多余的空格 # yes
spam(ham[1], {eggs: 2})
value my_list[index]# no spam( ham[1], { eggs : 2 } ) value my_list[ index ] 字典、列表对象的左括号之前不要多余的空格 # 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 比较 使用变量在左常量在右不显示进行对True、False的比较否定比较采用foo not in bar的形式而不是not foo in bar使用instance(a, C)进行实例的类型检查而不是type(A) is C # yes
if method md5: pass if not foo: pass if foo not in bar: pass if instance(a, C): pass # no if md5 method: pass if foo False: pass if not foo in bar: pass if type(A) is C: pass 引号 简单说自然语言使用双引号机器标示使用单引号因此 代码里 多数应该使用 单引号 自然语言 使用双引号 ...例如错误信息很多情况还是unicode使用u你好世界机器标识 使用单引号 ... 例如dict里的key正则表达式 使用原生的双引号 r...文档字符 使用三个双引号 ......import语句 import语句应该分行书写 # yes
import os
import sys# no import sys,os # yes from subprocess import Popen, PIPE import语句应该使用 absolute import # yes
from foo.bar import Bar# no from ..bar import Bar import语句应该放在文件头部置于模块说明及docstring之后于全局变量之前import语句应该按照顺序排列每组之间用空行分隔 import os
import sysimport msgpack
import zmqimport foo 导入其他模块的类定义时可以使用相对导入 from myclass import MyClass 如果发生命名冲突则可使用命名空间 import bar
import foo.barbar.Bar()
foo.bar.Bar() 注释 块注释 #号后空一格段落间用空行分开同样需要#号 # 块注释
# 块注释
#
# 块注释 # 块注释 行注释 至少使用两个空格和语句分开使用有意义的注释 # yes
x x 1 # 边框加粗一个像素 # no x x 1 # x加1 docstring docstring的规范在 PEP 257 中有详细描述其中最其本的两点 所有的公共模块、函数、类、方法都应该写docstring。私有方法不一定需要但应该在def后提供一个块注释来说明。docstring的结束应该独占一行除非此docstring只有一行。 Return a foobar
Optional plotz says to frobnicate the bizbaz first.
Oneline docstring 命名规范 应避免使用小写字母l(L)大写字母O(o)或I(i)单独作为一个变量的名称以区分数字1和0包和模块使用全小写命名尽量不要使用下划线类名使用CamelCase命名风格内部类可用一个下划线开头约定的缩写保留原样例如使用HTTPWriter而不是HttpWriter函数使用下划线分隔的小写命名lowercase_with_underscores当参数名称和Python保留字冲突可在最后添加一个下划线尽量不是使用缩写或自造的词常量使用以下划线分隔的大写命名UPPERCASE_WITH_UNDERSCORESPrecompiled regular expressions: name_re MAX_OVERFLOW 100Class FooBar:def foo_bar(self, print_): print(print_) Function and method arguments: Class methods: cls as first parameter.Instance methods: self as first parameter.Lambdas for properties may have the first parameter replaced with x, as in display_name property(lambda x: x.real_name or x.username).编码 文件使用UTF-8编码文件头部加入#-*-coding:utf-8-*-标识转载于:https://www.cnblogs.com/nullblogs/p/7612883.html