gpt4 book ai didi

python - 将一行 python 分成多行?

转载 作者:太空狗 更新时间:2023-10-29 17:35:45 25 4
gpt4 key购买 nike

在 C++ 中,如果代码行太长,或者如果 if 语句中有很多检查,我喜欢打断它们。

if (x == 10 && y < 20 && name == "hi" && obj1 != null) 
// Do things

// vs

if (x == 10
&& y < 20
&& name == "hi"
&& obj1 != null)
{
// Do things
}

AddAndSpawnParticleSystem(inParticleEffectName, inEffectIDHash, overrideParticleSystems, mAppliedEffects[inEffectIDHash], inTagNameHash);
// vs
AddAndSpawnParticleSystem(inParticleEffectName, inEffectIDHash, overrideParticleSystems,
mAppliedEffects[inEffectIDHash], inTagNameHash);

在 Python 中,代码块由选项卡定义,而不是“;”在行尾

if number > 5 and number < 15:
print "1"

在 python 中可以使用多行吗?喜欢...

if number > 5 
and number < 15:
print "1"

我不认为这是可能的,但它会很酷!

最佳答案

风格指南(PEP-8)说:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

方法一:使用括号

if (number > 5 and
number < 15):
print "1"

方法二:使用反斜杠

if number > 5 and \
number < 15:
print "1"

方法三:使用反斜杠+缩进提高可读性

if number > 5 and \
number < 15:
print "1"

关于python - 将一行 python 分成多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417571/

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