我不知道有这个,但我很高兴看到它。专业上我使用过 3.6(没有这个),并且有多个长行上下文管理器和black https://github.com/psf/black,格式化确实很难:
如果你有这个:
with some_really_long_context_manager(), and_something_else_makes_the_line_too_long():
pass
你必须这样写,这很难看:
with some_really_long_context_manager(), \
and_something_else_makes_the_line_too_long():
pass
如果你的论点太长:
with some_context(and_long_arguments), and_another(now_the_line_is_too_long):
pass
你可以这样做:
with some_context(and_long_arguments), and_another(
now_the_line_is_too_long
):
pass
但是,如果一个上下文管理器不接受参数,那么这不起作用,而且无论如何它看起来都有点奇怪:
with some_context(and_long_arguments), one_without_arguments(
), and_another(now_the_line_is_too_long):
pass
为此,您必须重新排列:
with some_context(and_long_arguments), and_another(
now_the_line_is_too_long
), one_without_arguments():
pass
使用新语法,可以将其格式化为:
with (
some_context(and_long_arguments),
one_without_arguments(),
and_another(now_the_line_is_too_long),
):
pass
这也使得 diff 更具可读性。