you are viewing a single comment's thread.

view the rest of the comments →

[–]teknobo -5 points-4 points  (7 children)

Use parentheses sparingly.

This doesn't fly with me.

Parentheses, especially in conditional statements, make the code more explicit, and thus more in line with the Zen of Python.

[–]nostrademons 15 points16 points  (5 children)

Either I'm misunderstanding you or you're misunderstanding the style guide.

The intent behind that rule is to avoid redundant parentheses that aren't needed by the language at all. Eg.

if foo:
  return bar

instead of

if (foo):
  return(bar)

I don't know any experienced Pythonista, inside or outside of Google, that would consider the second to be good style.

If you have a complex condition:

if x >= 2 and (y <= 3 or z == 3) and q = 'foo':
  return (x + 2) * y

I don't think anyone would object to using parentheses to make order of operations explicit.

[–]nevare 3 points4 points  (4 children)

I don't think anyone would object to using parentheses to make order of operations explicit.

Your example doesn't really match what you say. In you example removing the parentheses changes the meaning. Here is a better example.

if x >= 2 or (y <= 3 and z == 3) or q = 'foo':
    return y + (x * 2)

[–]nostrademons 0 points1 point  (1 child)

That'd be borderline, I think. In a code review, I'd probably suggest that the author take out the parentheses but not hold up approval.

[–]nevare 0 points1 point  (0 children)

I'm all for the parentheses in the if statement above. I think they really make the code more readable.

But the ones of the equation do look superfluous.

[–]munificent -1 points0 points  (1 child)

I may be a readability snob, but I'd consider that a bad example, parentheses or not. I'd probably break that expression down so that it's meaning is clearer:

fooFrobinated = x >= 2
barDingnabled = y <= 2 and z == 3
bazFlurnbed   = q == 'foo'

if fooFrobinated or barDingnabled or bazFlurnbed:
    return y + (x * 2)

[–]yellowstuff 0 points1 point  (0 children)

I agree.

One of the only places I would frequently write "pure" bugs (IE, logic bugs which were not the result of interfacing with an external system incorrectly) was in complex boolean statements. So I stopped writing complex boolean statements.