gpt4 book ai didi

python - 是否可以在 Python 中将长行分成多行?

转载 作者:IT老高 更新时间:2023-10-28 12:01:58 25 4
gpt4 key购买 nike

就像 C 一样,您可以将一条长线分成多条短线。但是在 Python ,如果我这样做,会出现缩进错误...有可能吗?

最佳答案

来自 PEP 8 - Style Guide for Python Code :

The preferred way of wrapping long lines is by using Python's implied linecontinuation inside parentheses, brackets and braces. If necessary, youcan add an extra pair of parentheses around an expression, but sometimesusing a backslash looks better. Make sure to indent the continued lineappropriately.

隐式续行示例:

a = some_function(
'1' + '2' + '3' - '4')

关于二元运算符周围的换行符,它继续说:

For decades the recommended style was to break after binary operators.But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line.

In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style (line breaks before the operator) is suggested.

显式续行示例:

a = '1'   \
+ '2' \
+ '3' \
- '4'

关于python - 是否可以在 Python 中将长行分成多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4172448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com