我正在使用spyder,并且我有一个类似这样的代码
detector_x, detector_y, smeared_x, smeared_y = gamma_detection(decay_positions, cos_theta, phi)
对于第二个i
在decay_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/