我正在使用spyder,我有一个这样的代码
detector_x, detector_y, smeared_x, smeared_y = gamma_detection(decay_positions, cos_theta, phi)
对于其中,decay_positions
中的第二个i
超过了一行中建议的字符数(大约 90 个(。我有动态 PEP8 分析,所以它自然会为我的代码分析提供警告。那么在这种情况下,正确的 PEP8 方法是什么?是吗
detector_x, detector_y, smeared_x, smeared_y =
gamma_detection(decay_positions, cos_theta, phi)
从技术上讲,它仍在运行,但它给了我警告
E122 continuation line missing indentation or outdented
还是
detector_x, detector_y, smeared_x, smeared_y = gamma_detection(
decay_positions, cos_theta, phi)
还是
detector_x, detector_y, smeared_x, smeared_y = gamma_detection(
decay_positions, cos_theta, phi)
您提供了选项 {1, 2, 3}。
一定要使用 2 或 3。这是他们之间的品味问题,或者您最喜欢的编辑器鼓励您使用的内容。(将它们全部放在一行上,光标经过打开的括号,点击 RETURN,然后使用建议的缩进。只要
$ flake8
不抱怨,你是金子。(用pip install flake8
获取它。
对于很多参数,或涉及冗长表达式的参数,您甚至可以考虑每行列出一个参数。
如果要分配给大量长标识符,您可以考虑将 LHS 包围在元组解包的括号中:
(detector_x, detector_y,
smeared_x, smeared_y) = gamma_detection(
decay_positions,
cos_theta + epsilon * some_long_expression(),
phi)
在 VS 代码上使用 autopep8,您的代码将更改为以下内容:
detector_x, detector_y, smeared_x, smeared_y =
gamma_detection(decay_positions, cos_theta, phi)
现在我看了一下总长度,如果这是一个单行,即 96 个字符。即使这超过了 ~79 个字符的 PEP8 限制,我仍然建议将其保留为一行。实际上,79 个字符非常短,如果您制作 100 个字符的行,可读性不会降低。在我工作的地方,我们还将行长准则增加到 100 个字符。
# 96 characters
detector_x, detector_y, smeared_x, smeared_y = gamma_detection(decay_positions, cos_theta, phi)