[Python-ideas] Potential PEP: with/except

Paul Ferrell pflarr at gmail.com
Tue Jan 22 17:48:27 EST 2019

I completely understand your perspective, and agree with most of it. It
doesn't add new expressiveness, it adds a bit of polish (and I think
completeness) to the relatively new concept of 'with' statements.

Is this so intuitive that we don't actually have to teach it?
Is it such a natural extension to 'with', that it would immediately be
weird to find it missing in the future?

If the answer to either of those questions is 'no', then I absolutely
retract my idea.

On Tue, Jan 22, 2019 at 3:22 PM Steven D'Aprano <steve at pearwood.info> wrote:

> On Tue, Jan 22, 2019 at 01:11:10PM -0700, Paul Ferrell wrote:
> [...]
> > I would like to propose that the syntax for 'with' blocks
> > be changed such that they can be accompanied by 'except', 'finally',
> > and/or 'else' blocks as per a standard 'try' block.
> What benefit does this give apart from saving one line and one indent?
> If either is in short supply, the code probably needs refactoring, not
> new syntax.
> The beauty of the current syntax is that try...except and with blocks
> are fully independent, composable blocks which can be learned and
> reasoned about seperately. You're proposing to add a new special-case
> syntax:
>     while ...
>     except ...
> that adds a new block structure that has to be implemented, documented,
> tested, maintained, taught and learned. It will inevitably lead to
> questions on mailing lists, IRC and Stackoverflow asking what is the
> difference between a separate try...with...except and a with...except,
> and when to choose one or the other.
> And of course then there will be the inevitable requests that we
> generalise it to other blocks:
>     for ...
>     except ...
>     while ...
>     except ...
> If this will allow us to write more expressive code, or do things we
> couldn't easily do before, then it might be worthwhile to add this
> additional complexity.
> But if all it does is save one line and one indent, then I believe it is
> redundant and I would be against it.
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

Paul Ferrell
pflarr at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190122/6aaf1119/attachment-0001.html>

More information about the Python-ideas mailing list