Python supports parentheses in many statements:
from requests import (models, api,
auth)
# No error
with (open('README.md') as f,
open('setup.py') as g):
pass
# No error
a = b = c = d = e = f = 0
del (a, b, c,
d, e, f)
# No error
However, the global and nonlocal statement currently does not support the use of parentheses, as demonstrated in this example:
def foo():
global (a, b, c,
d, e, f)
# SyntaxError: invalid syntax
def foo():
a = b = c = d = e = f = 0
def bar():
nonlocal (a, b, c,
d, e, f)
a, b, c, d, e, f = range(6)
return bar
# SyntaxError: invalid syntax
Adding parentheses support to the global
and nonlocal
statements would make Python more consistent. In the meantime, a practical solution for implementing multiline global statements is by utilizing a backslash, as shown below:
def foo():
global \
a, b, c, \
d, e, f
# No error
def foo():
a = b = c = d = e = f = 0
def bar():
nonlocal \
a, b, c, \
d, e, f
a, b, c, d, e, f = range(6)
return bar
# No error
Itâs worth mentioning that backslashes can be employed in other statements as well, as evidenced in the following examples:
from requests import \
models, api, \
auth
# No error
with open('README.md') as f, \
open('setup.py') as g:
pass
# No error
a = b = c = d = e = f = 0
del \
a, b, c,\
d, e, f
# No error
Adding parentheses support for the global
and nonlocal
statement would provide more consistency. Though it is currently possible to use a backslash for multiline purposes, incorporating parentheses could lead to a smoother and more polished coding experience.