编码风格 - 将括号保留在同一行还是新行?
假设您正在调用一个函数,为了可读性,显然需要将语句分解为几行。但是,至少有两种方法可以做到这一点:
您会这样做:
return render(request, template,
{
'var1' : value1,
'var2' : value2,
'var3' : value3
}
)
或者您宁愿这样做:
return render \
(
request, template,
{
'var1' : value1,
'var2' : value2,
'var3' : value3
}
)
或者,请建议您自己的格式。另请列出您使用特定格式的原因以及其他格式的问题。
谢谢
Suppose you are calling a function, where there's clearly a need to break down the statement into few lines, for readability's sake. However there are at least two way to do it:
Would you do this:
return render(request, template,
{
'var1' : value1,
'var2' : value2,
'var3' : value3
}
)
Or would you rather do that:
return render \
(
request, template,
{
'var1' : value1,
'var2' : value2,
'var3' : value3
}
)
Or, please suggest your own formatting. Please also list reasons why would you use a particular formatting and what's wrong with the other one.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可能会这样做:
我会将括号保留在同一行,以便搜索
render(
工作。而且因为我发现它更清晰。但我会将所有参数放在新行上。I'd probably do:
I would keep the bracket on the same line, so that searches for
render(
work. And because I find it clearer. But I'd put all the arguments on new lines.Python 官方 PEP-8 建议第一个。
Python's official PEP-8 suggests the first one.
我会这样做:
I would do:
第二个看起来像是从 C[#+]* 程序中转义的。反斜杠行延续很丑陋,容易出现尾随空格问题,并且当您需要使用 () 或 [] 时,没有理由使用它。
The second one looks like it escaped from a C[#+]* program. Backslash line continuation is ugly, prone to trouble with trailing space, and there's no excuse to use it when you've got () or [] to use.