我该如何换行(换行)? [英] How can I do a line break (line continuation)?

查看:49
本文介绍了我该如何换行(换行)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长行代码,我想在多行中分解.我使用什么,语法是什么?

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?

例如,添加一串字符串,

For example, adding a bunch of strings,

e = 'a' + 'b' + 'c' + 'd'

并将其分成两行,如下所示:

and have it in two lines like this:

e = 'a' + 'b' +
    'c' + 'd'

推荐答案

哪一行?您只需在下一行就有参数就可以了,没有任何问题:

What is the line? You can just have arguments on the next line without any problems:

a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, 
            blahblah6, blahblah7)

否则,您可以执行以下操作:

Otherwise you can do something like this:

if (a == True and
    b == False):

或带有明确的换行符:

if a == True and \
   b == False:

查看样式指南以获取更多信息.

使用括号,您的示例可以写成多行:

Using parentheses, your example can be written over multiple lines:

a = ('1' + '2' + '3' +
    '4' + '5')

使用显式换行符可以获得相同的效果:

The same effect can be obtained using explicit line break:

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

请注意,样式指南指出,最好使用带括号的隐式连续,但是在这种特殊情况下,仅在表达式周围加上括号可能是错误的方法.

Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.

这篇关于我该如何换行(换行)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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