foo_bar(self, width, height, color='黑', design=None, x='foo',
emphasis=None, highlight=0)
if (width == 0 and height == 0 and
color == '红' and emphasis == '加粗'):
(bridge_questions.clarification_on
.average_airspeed_of.unladen_swallow) = '美国的还是欧洲的?'
with (
very_long_first_expression_function() as spam,
very_long_second_expression_function() as beans,
third_thing() as eggs,
place_order(eggs, beans, spam, beans)
if width == 0 and height == 0 and \
color == '红' and emphasis == '加粗':
bridge_questions.clarification_on \
.average_airspeed_of.unladen_swallow = '美国的还是欧洲的?'
with very_long_first_expression_function() as spam, \
very_long_second_expression_function() as beans, \
third_thing() as eggs:
place_order(eggs, beans, spam, beans)
如果字符串的字面量 (literal) 超过一行, 应该用圆括号实现隐式续行:
x = ('这是一个很长很长很长很长很长很长'
'很长很长很长很长很长的字符串')
最好在最外层的语法结构上分行. 如果你需要多次换行, 应该在同一层语法结构上换行.
bridgekeeper.answer(
name="亚瑟", quest=questlib.find(owner="亚瑟", perilous=True))
answer = (a_long_line().of_chained_methods()
.that_eventually_provides().an_answer())
if (
config is None
or 'editor.language' not in config
or config['editor.language'].use_spaces is False
use_tabs()
bridgekeeper.answer(name="亚瑟", quest=questlib.find(
owner="亚瑟", perilous=True))
answer = a_long_line().of_chained_methods().that_eventually_provides(
).an_answer()
if (config is None or 'editor.language' not in config or config[
'editor.language'].use_spaces is False):
use_tabs()
必要时, 注释中的长 URL 可以独立成行.
# 详情参见
# http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html
# 详情参见
# http://www.example.com/us/developer/documentation/api/content/\
# v2.0/csv_file_name_extension_full_specification.html
注意上面各个例子中的缩进; 详情参见 缩进 章节的解释.
如果一行超过 80 个字符, 且 Black 或 Pyink 自动格式化工具无法继续缩减行宽, 则允许该行超过 80 个字符. 我们也鼓励作者根据上面的规则手动拆分.
使用括号时宁缺毋滥.
可以把元组 (tuple) 括起来, 但不强制. 不要在返回语句或条件语句中使用括号, 除非用于隐式续行或表示元组.
if foo:
bar()
while x:
x = bar()
if x and y:
bar()
if not x:
bar()
# 对于包含单个元素的元组, 括号比逗号更直观.
onesie = (foo,)
return foo
return spam, beans
return (spam, beans)
for (x, y) in dict.items(): ...
if (x):
bar()
if not(x):
bar()
return (foo)
用4个空格作为缩进.
不要使用制表符. 使用隐式续行时, 应该把括起来的元素垂直对齐(参见 行宽 章节的示例), 或者添加4个空格的悬挂缩进. 右括号 (圆括号, 方括号或花括号) 可以置于表达式结尾或者另起一行. 另起一行时右括号应该和左括号所在的那一行缩进相同.
# 与左括号对齐.
foo = long_function_name(var_one, var_two,
var_three, var_four)
meal = (spam,
beans)
# 与字典的左括号对齐.
foo = {
'long_dictionary_key': value1 +
value2,
# 4个空格的悬挂缩进; 首行没有元素
foo = long_function_name(
var_one, var_two, var_three,
var_four)
meal = (
spam,
beans)
# 4个空格的悬挂缩进; 首行没有元素
# 右括号另起一行.
foo = long_function_name(
var_one, var_two, var_three,
var_four
meal = (
spam,
beans,
# 字典中的4空格悬挂缩进.
foo = {
'long_dictionary_key':
long_dictionary_value,
# 首行不能有元素.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 禁止2个空格的悬挂缩进.
foo = long_function_name(
var_one, var_two, var_three,
var_four)
# 字典没有悬挂缩进.
foo = {
'long_dictionary_key':
long_dictionary_value,
大部分 .py
文件不必以 #!
开始. 可以根据 PEP-394 , 在程序的主文件开头添加 #!/usr/bin/env python3
(以支持 virtualenv) 或者 #!/usr/bin/python3
.
(译者注: 在计算机科学中, Shebang (也称为Hashbang)是一个由井号和叹号构成的字符串行(#!), 其出现在文本文件的第一行的前两个字符. 在文件中存在Shebang的情况下, 类Unix操作系统的程序载入器会分析Shebang后的内容, 将这些内容作为解释器指令, 并调用该指令, 并将载有Shebang的文件路径作为该解释器的参数. 例如, 以指令#!/bin/sh开头的文件在执行时会实际调用/bin/sh程序.)
内核会通过这行内容找到Python解释器, 但是Python解释器在导入模块时会忽略这行内容. 这行内容仅对需要直接运行的文件有效.