[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Tue Feb 18 00:54:50 CET 2014


On Tue, Feb 18, 2014 at 9:26 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> There's no logical difference between not generating
> a result at all, and generating a result of None and
> then throwing it away.
>
> The same thing applies here:
>
>    menu.remove(mint) except ValueError: pass
>
> This says exactly what the programmer means: "Remove
> mint from the menu if it's there, otherwise do nothing."

In a statement context, it's possible to say "otherwise do nothing".
An if without an else does this, as does putting 'pass' in certain
places.

But in an expression context, there are 'two possibility'. Either some
kind of value is returned, or an exception is raised. (At least, I
don't think there are any other options. Harry Hoo, not the Spanish
Inquisition.) What should happen here:

func(menu.remove(mint) except ValueError: pass)

If remove() raises ValueError, should func be called? If so, with what
argument? (Note that it's because English is happy with "passing"
arguments to functions that I was happy with the notation "except
ValueError pass None", but that's using pass as a syntactic element,
in place of the colon.)

ChrisA


More information about the Python-ideas mailing list