# (垂直隐式缩进)对准左括号
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', )
# 无括号续行, 利用反斜杠
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))
# 类的方法定义用单个空行分割,两行空行分割顶层函数和类的定义。 class A(object): def method1(): pass def method2(): pass def method3(): pass
# 按模块首字母排序导入, 依此递推 import active import adidas import create
# 一行导入多模块 import sys, os, knife # 不按首字母导入 import create import active import beyond
# 单引号和双引号效果一样 name = 'JmilkFan' name = "Hey Guys!"
spam(ham[1], {eggs: 2})
spam( ham[ 1 ], { eggs: 2 } )
if x == 4: print x, y; x, y = y, x
if x == 4 : print x , y ; x , y = y , x
spam(1) dct['key'] = lst[index]
spam (1) dct ['key'] = lst [index]
x = 1 y = 2 long_variable = 3
x = 1 y = 2 long_variable = 3
a = b a or b # 括号内的操作符不需要空格 name = get_name(age, sex=None, city=Beijing)
# Have to define the param `args(List)`, # otherwise will be capture the CLI option when execute `python manage.py server`. # oslo_config: (args if args is not None else sys.argv[1:]) CONF(args=[], default_config_files=[CONFIG_FILE])
x = x + 1 # Compensate for border
# 多行文档, 首行首字母大写,结尾的 """ 应该单独成行 """Return a foobang Optional plotz says to frobnicate the bizbaz first. """ # 单行的文档, 结尾的 """ 在同一行。 """Return a foobang"""
class MyClass(object): pass
_name = 'name'
vcenter_connection = ''
MAX_OVERFLOW = '' TOTAL = 1
# Yes if foo is not None # No if not foo is None
# Yes def f(x): return 2*x # No f = lambda x: 2*x
# Yes (捕获具体异常) try: import platform_specific_module except ImportError: platform_specific_module = None # No (不要全局捕获) try: import platform_specific_module except: platform_specific_module = None
# Yes try: value = collection[key] except KeyError: return key_not_found(key) else: return handle_value(value) # No try: return handle_value(collection[key]) except KeyError: # 可能会捕捉到 handle_value()中的 KeyError, 而不是 collection 的 return key_not_found(key)
# Yes def foo(): return None # No def foo(): return
# Yes
if foo.startswith('bar'):
# No
if foo[:3] == 'bar':
# Yes if isinstance(obj, int): # No if type(obj) is type(1):
# Yes if not seq: pass if seq: pass # No if len(seq): pass if not len(seq): pass
# Yes if greeting: pass # No if greeting == True pass if greeting is True: # Worse pass
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有