Quantum Mechanic
Quantum Mechanic

Reputation: 662

Pep8 and multiline while conditions

I had to add extraneous parens to a while condition today to avoid pep8 complaints:

while not found and not something and \
    (time_left is None or time_left > 0):
    (one, two, three, four) = self.gimme(timeout=time_left)

My solution:

while (not found and not something and
       (time_left is None or time_left > 0)):
    (one, two, three, four) = self.gimme(timeout=time_left)

If I changed the 2nd line indent, it complained of over-indent or missing indent, for every indent from even with the W in while, to 8 to the right of it.

I'm bothered that adding extraneous parens to satisfy pep8, for little readability improvement, goes against general principles.

Any ideas? Have I missed a better solution?

Upvotes: 2

Views: 3686

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385850

I think the best solution is to do whatever you (and your team, if applicable) considers to be the most readable solution. PEP8 is just a guideline, it isn't a requirement. Focus on writing code that is robust and easy to comprehend.

Upvotes: 0

kofrezo
kofrezo

Reputation: 71

I prefer to break the long lines after conditional statements to increase readability. e.g.:

while (
    not found and 
    not something and 
    (time_left is None or time_left > 9)
):
    (one, two, three, four) = self.gimme(timeout=time_left)

I think that is very readable and at least satisfies my pep8 codestyle check.

Upvotes: 7

Related Questions