[Python-ideas] except expression

Ron Adam ron3200 at gmail.com
Thu Feb 20 16:26:43 CET 2014


On 02/20/2014 07:41 AM, אלעזר wrote:
>
> 2014-02-20 15:31 GMT+02:00 Chris Angelico
> <rosuav at gmail.com
> <mailto:rosuav at gmail.com>>:
>  >
>  > On Fri, Feb 21, 2014 at 12:23 AM, Ron Adam
> <ron3200 at gmail.com
> <mailto:ron3200 at gmail.com>> wrote:
>  > > The expression doesn't need to be just too terms, it could be
> something more
>  > > complex.
>  > >
>  > >       (except KeyError: d1[key] or d2[key] or d3[key] or None)
>  > >
>  > >
>  > > Would give the first dictionary lookup that doesn't raise KeyError or
> None.
>  > >
>  > > Because the exception paths and data paths don't overlap, they could be
>  > > dictionaries containing exception instances and it would still work.
>  >
>  > Okay. I think I follow. The way to spell that in the current proposal is:
>  >
>  > d1[key] except KeyError: (d2[key] except KeyError: (d3[key] except
>  > KeyError: None))
>  >
>  > which is rather more verbose. On the other hand, the syntax you have
>  > requires magic around the 'or' keyword.
>  >
> Perhaps it should be a colon-seperated list:
>
>      (except KeyError: d1[key] : d2[key] : d3[key] : None)
>
> Or maybe a semicolon.
>
>      (except KeyError: d1[key] ; d2[key] ; d3[key] ; None)

The semicolon corresponds to "and statement" in it's normal use and has no 
return value.

How about re-using the binary operators...

        (except KeyError try d1[key] | d2[key] | d3[key] | None)


It's not uncommon for symbols to mean different things in different places.

    %, +, *

There are probably others I'm not thinking of at the moment.

I really like this actually and it makes the syntax even more concise.  :-)

Cheers,
    Ron





















More information about the Python-ideas mailing list