将一行python拆分为多行? [英] Breaking a line of python to multiple lines?

查看:36
本文介绍了将一行python拆分为多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中,如果代码行太长,或者如果其中包含大量检查,我喜欢拆分 if 语句.

In C++, I like to break up my lines of code if they get too long, or if an if statement if there are a lot of checks in it.

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 中,代码块由制表符定义,而不是由;"在行尾

In Python, code blocks are defined by the tabs, not by the ";" at the end of the line

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

python 中可以有多行吗?喜欢...

Is mutliple lines possible in python? like...

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

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

I don't think this is possible, but it would be cool!

推荐答案

样式指南说:

包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行继续符.通过将表达式括在括号中,可以将长行分成多行.这些应该优先于使用反斜杠进行行延续.确保适当缩进连续行.打破二元运算符的首选位置是在运算符之后,而不是在它之前.

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"

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

Method 3: Using backslash + indent for better readability

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

这篇关于将一行python拆分为多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆