[Python-ideas] Allow parentheses to be used with "with" block

Ron Adam ron3200 at gmail.com
Mon Feb 16 17:58:35 CET 2015



On 02/16/2015 12:32 AM, Greg Ewing wrote:
> I *think* this is unambiguous:
>
>     with a as b and c as d and e as f:
>        ...
>
> because the rule for a with statement is
>
> with_stmt: 'with' with_item (',' with_item)*  ':' suite
> with_item: test ['as' expr]
>
> and expr doesn't include 'and'.

That may be unambiguous to the parser, but it uses "and" in a new way.

     with (a as b) and (c as d) and (e as f):

The with would get only (e as f) if the "and"s were interpreted normally. 
That is if (a as b) didn't raise an exception or return 0 or False.



A possible way is to make "as" a valid expression on it's own.

     (expr as name)  <-->  ("name", expr)

So "as" can becomes a way to create (key, value) pairs.


And then change "with" to take a single (key, value) pair, or a tuple of 
(key, value) pairs, and then this will just work ... ;-)

    with (e1 as a,
          e2 as b,
          e3 as c):
        ...


The two issues here is how it would effect error messages for the "with" 
statement, and imports use of "as" would still be a bit special.


Cheers,
    Ron


























Cheers,
    Ron















More information about the Python-ideas mailing list