How can I do a line break (line continuation) in Python?

后端 未结 11 1522
囚心锁ツ
囚心锁ツ 2020-11-21 22:56

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,



        
相关标签:
11条回答
  • 2020-11-21 23:46

    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.

    0 讨论(0)
  • 2020-11-21 23:50

    You can break lines in between parenthesises and braces. Additionally, you can append the backslash character \ to a line to explicitly break it:

    x = (tuples_first_value,
         second_value)
    y = 1 + \
        2
    
    0 讨论(0)
  • 2020-11-21 23:51

    Taken from The Hitchhiker's Guide to Python (Line Continuation):

    When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the backslash, will break the code and may have unexpected results.

    A better solution is to use parentheses around your elements. Left with an unclosed parenthesis on an end-of-line the Python interpreter will join the next line until the parentheses are closed. The same behaviour holds for curly and square braces.

    However, more often than not, having to split a long logical line is a sign that you are trying to do too many things at the same time, which may hinder readability.

    Having that said, here's an example considering multiple imports (when exceeding line limits, defined on PEP-8), also applied to strings in general:

    from app import (
        app, abort, make_response, redirect, render_template, request, session
    )
    
    0 讨论(0)
  • 2020-11-21 23:52

    It may not be the Pythonic way, but I generally use a list with the join function for writing a long string, like SQL queries:

    query = " ".join([
        'SELECT * FROM "TableName"',
        'WHERE "SomeColumn1"=VALUE',
        'ORDER BY "SomeColumn2"',
        'LIMIT 5;'
    ])
    
    0 讨论(0)
  • 2020-11-21 23:53

    From the horse's mouth: Explicit line joining

    Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example:

    if 1900 < year < 2100 and 1 <= month <= 12 \
       and 1 <= day <= 31 and 0 <= hour < 24 \
       and 0 <= minute < 60 and 0 <= second < 60:   # Looks like a valid date
            return 1
    

    A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on a line outside a string literal.

    0 讨论(0)
提交回复
热议问题