Oscar Benjamin writes:
 > On Sun, 7 Feb 2021 at 23:55, Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
 > > On 8/02/21 6:59 am, Christopher Barker wrote:

 > > > I find myself
 > > I often write
 > My preferred option is

There's ALWAYS more than one way to do it! :-)

I'm not a fan of the proposed new syntax.  A big issue for me is that
"with" doesn't commute with "if".  That is Chris will write

    with open('file') as f:
        lines = r.readlines()
    for line in lines:
        process(line)

instead of

    with open('file') as f:
        for line in f:
            process(line)

but AFAICS the proposed syntax doesn't help by itself:

    for line in (what?) with open('file'):
        process(line)

and

    for line in f with open('file') as f:
        process(line)

will be horrible with any more complicated context manager constructor
IMO, YMMV.  Note that I'm assuming a similar facility for 'for', for
convenience of exposition.  Nobody has proposed it yet, I think, just
mentioned it.  I guess you could do as Jonathan's code does:

    f = open('file', and, many, more, arguments)
    for line in f with f:
        process(line)

but I'm not impressed by that syntax.  In the case in Jonathan's post,

    if stamp.expired():
        with stamp:
            [code]

is TRT, but I wonder how often it is, compared to cases where

    with stamp:
        if stamp.expired():
            [code]

DTRTs.  Notice that once you combine them into a single statement, you
can choose either semantics, which is a bug magnet.

Of course you could try allowing both "if ... with ..." and "with
... if ..." with the evident semantics, but "with thing if" already
has a meaning ("thing if" introduces a conditional expression).
Although the PEG parser can presumably handle it, I'm not a fan of
parsing "with thing if" as a combined with/if statement if the else
arm is missing, and otherwise as a with/conditional expression
statement.

Bottom line: IMO this syntax probably should be limited to the
particular case where there's a context manager that doesn't need to
be referred to in the block, and the nesting is if = outer, with =
inner.  I don't think that's enough to justify new syntax, merely to
save one level of indentation that can be saved in a large number of
ways that probably cover most of the use cases.

Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MSZYZT4HLZXQ7Q5SE3D2VA7PUIGYMQKK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to