python - 从函数调用常量时,(PEP8)换行的正确方法是什么?

标签 python python-3.x pep8 code-structure

我正在使用spyder,并且我有一个类似这样的代码

    detector_x, detector_y, smeared_x, smeared_y = gamma_detection(decay_positions, cos_theta, phi)

对于第二个idecay_positions超过推荐的一行字符数(例如 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)

关于python - 从函数调用常量时,(PEP8)换行的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55366203/

相关文章:

python-3.x - 如何在使用带有 for 循环的请求时忽略 HTTP 错误?

python - __init__ 可以用作正常的初始化方法,而不是构造函数吗?

python - PEP8 逗号后空格

python - 计算两个函数的重叠面积

python - 乘以无循环的矩阵列表

python - 在 pandas python 中将列值转换为行值时出现问题?

python - 使用列表中多个字典的条件从键中检索项目

python - 诗歌即使存在也找不到依赖的版本

python - MacOS X 上的 pip install lxml 给出错误 : command 'cc' failed

python - 在 unicode 中,Pytest 无法生成 XML 输出