Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

python - 如何在Python中进行换行(换行)?(How can I do a line break (line continuation) in Python?)

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'
  ask by Ray Vega translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Check the style guide for more information.

(查看样式指南以获取更多信息。)

From your example line:

(从示例行中:)

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

Or:

(要么:)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...