Friday, May 24, 2019

syntax - 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'


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...