Parentheses in global statement

I am testing this in the Python interpreter.

While parentheses are redundant, interpreter doesn’t raise a syntax error. However, removing the parentheses does result in a syntax error. This is yet another inconsistency.

This approach functions with the “return” statement.

a = 4
b = 4


def foo():
    return ((((a == 4, b == 4))))


print(foo())  # (True, True)

However, let’s stay on track!
I don’t see any reasons against using parentheses to join lines, especially when backslashes are already allowed for global and nonlocal statements.

I understand that. But the Python interpreter can’t reasonably do anything to help you with code that might be erroneous. A linter can warn you.

Assertions take an “expression”, and a parenthesized expression with a comma is a tuple. So you can’t also use it to “join lines” in the way that you mean (combining multiple assertions). Anyway, combining multiple assertions together is just bad.

That said, you can join lines in the usual way with parentheses and assertions:

assert (
  123
)

… which, since it will not be empty, will always be true, thus rather weakening the value of the assertion.

Right, and linters will pick that up too.

This discussion seem to be getting unproductively heated. Everyone please take a deep breath. Someone on the internet is wrong! It could be you! :slight_smile:

4 Likes