11

I use the ninja-ide and I find amazing the way it can complain about everything (yellow undertext line everywhere) because I think its a way to improve my coding to make it more standard.

However, since it does also complain about the length of the line of code (which of course makes a lot of sense, since no one likes to scroll horizontally to read the code), I've got stuck to this problem.

Lets say this line:

 v1, v2 = np.sum(((b1 - m1) ** 2) * p1) / q1, np.sum(((b2 - m2) ** 2) * p2) / q2

It does have 81 characters including the spaces, in this case I could split it like this:

    v1 = np.sum(((b1 - m1) ** 2) * p1) / q1
    v2 = np.sum(((b2 - m2) ** 2) * p2) / q2

But that doesn't feel much pythonic and there is also another problem:

What if there wasn't the comma? I mean how could I split something like this:

   v2 = np.sum(((b1 - m1) ** 2 * np.sum(((b2 - np.sum(((b2 - m2) ** 2) * p2) / q2) ** 2) * p2) / q2) * p1) 

This above doesn't have any sense mathematically wise, it's just to explain what I meant.

Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • 2
    possible duplicate of [How can I do a line break (line continuation) in Python?](http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python) – Burhan Khalid Dec 04 '13 at 05:16
  • 2
    @BurhanKhalid I disagree, there is much more to this question than a simple line break – jfa Dec 04 '13 at 05:41

2 Answers2

7

A backslash can be used to split long lines.

Where you split it is up to you, but the above line could be reworked as such:

v2 = np.sum(((b1 - m1) ** 2 * \
        np.sum(((b2 - np.sum(((b2 - m2) ** 2) * p2) / q2) ** 2) * p2) \
                                                                / q2) * p1)

I'd first try to reduce the code some other way (e.g. renaming variables, using other modules, altering the order of operations).


@user2357112 correctly notes that you don't need the backslash when splitting code inside unmatched parentheses, brackets, or braces, so the above code could also look like this:

v2 = np.sum(((b1 - m1) ** 2 *
        np.sum(((b2 - np.sum(((b2 - m2) ** 2) * p2) / q2) ** 2) * p2)
                                                                / q2) * p1)

From PEP 8's Maximum Line Length:

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.

fgb
  • 3,009
  • 1
  • 18
  • 23
  • 1
    You don't actually need the backslash. Newlines inside unmatched parentheses, brackets, or braces are ignored. – user2357112 Dec 04 '13 at 05:54
  • @user2357112: Good point. I still use them for every line break so as not to worry whether there's an enclosing (/[/{ to handle the line break for me. – fgb Dec 04 '13 at 05:59
  • If you break at semantically sensible locations, generally between the biggest "chunks" possible, it tends to be pretty obvious whether there are appropriate unmatched tokens around the break. PEP 8 recommends this implicit line continuation over semicolons. – user2357112 Dec 04 '13 at 06:03
  • @user2357112: Another great point. I guess it's time for me to clean up some of my code ;) – fgb Dec 04 '13 at 06:07
4

Don't bother about pythonic-way. Bother about good reading, performance and extensibility of your code. I think below would good 'pythonic' :) solution:

formula = lambda b, m, p, q: np.sum(((b - m) ** 2) * p) / q
v1, v2 = formula(b1, m1, p1, q1), formula(b2, m2, p2, q2)

Advantage of this way is you can use formula many times. And you get shortest string.

Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25