18

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);

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"

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!

Eric
  • 95,302
  • 53
  • 242
  • 374
MintyAnt
  • 2,978
  • 7
  • 25
  • 37
  • 6
    Well... `if 5 < number < 15:` – Ry- Jan 19 '13 at 18:45
  • 3
    Put `\\` at the end of the line – Kabie Jan 19 '13 at 18:47
  • 3
    Code blocks aren't defined by tabs (or a fixed number of spaces), they're defined by any indentation. Also, that isn't your problem here, your problem is that newlines usually end statements. –  Jan 19 '13 at 18:49

4 Answers4

42

Style guide (PEP-8) says:

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.

Method 1: Using parenthesis

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

Method 2: Using backslash

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

Method 3: Using backslash + indent for better readability

if number > 5 and \
        number < 15:
    print "1"
Wasi Master
  • 1,112
  • 2
  • 11
  • 22
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • You might want to take a look at your code on one line: `if number > 5 and and number < 15:` –  Jan 19 '13 at 18:54
  • +1 however I would be tempted to move the order around so the preferred parenthesis example is shown first and then the alternative also valid syntax shown after. – Christopher Hackett Jan 19 '13 at 19:10
12

You can break an expression up into multiple lines if you surround it with parentheses:

if (x == 10
    and y < 20
    and name == "hi"
    and obj1 is not None):
    # do something

The same is true of brackets or curly braces used to create a list or dictionary:

mylist = [1, 2, 3, 4,
          5, 6, 7, 8]

mydict = {1: "a", 2: "b",
          3: "c", 4: "d"}
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • 1
    +1, this always looks a lot nicer than EOL escapes. (But `!= null` isn't quite Python `:)`) – Ry- Jan 19 '13 at 18:48
3

The pep8 standard guide seems to indented new lines for a list of things in parenthesis while for a long line they suggest a backslash at the end of the line.

Indenting new lines

Backslashes at the end of the line

JonathanV
  • 2,484
  • 1
  • 13
  • 9
1

You can place \ symbol to escape end-of-line:

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

In some cases (e.g. inside parentheses) you'll need no special symbols to escape end of line.

See more in documentation on python lexical analysis:

The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.

Vladimir
  • 9,913
  • 4
  • 26
  • 37