[Python-ideas] with-statement syntactic quirk

Terry Reedy tjreedy at udel.edu
Thu Nov 1 17:33:07 CET 2012

On 11/1/2012 6:48 AM, Antoine Pitrou wrote:

> Uh, what people seem to miss is that it's not only about nested context
> managers. It can happen with a single context manager:
> with (some_context_manager(many_arguments...)
>        as my_variable):
>      ...
> # SyntaxError!

As it should be. With is not a(n overt) function and with statements 
should not look like function calls. With clauses are not expressions, 
so they cannot (should not) be arbitrarily surrounded by parentheses as 
expressions can. Multiple with clauses do not form a tuple and do not 
need parentheses to set off the tuple and modify expression precedence.

I understand better now why I did not like adding optional \-avoidance 
parentheses to import statements. Sprinkling statements with optional, 
non-expression parentheses does not fit the nature of statement syntax.

Terry Jan Reedy

More information about the Python-ideas mailing list