[Python-ideas] except expression

Andrew Barnert abarnert at yahoo.com
Thu Feb 13 20:19:27 CET 2014


On Feb 13, 2014, at 10:45, spir <denis.spir at gmail.com> wrote:

> On 02/13/2014 02:26 PM, Rob Cliffe wrote:
>> It certainly feels right for the order to be  normal value, exception, default
>> value.  So the syntax I would like is
>>     x = entries[0] except IndexError XXX None
>> where XXX is some keyword.  Ideally 'then' or perhaps 'when' which read better
>> than 'else', but I understand adding a new keyword is a big deal.
>> (FWIW I also wish trinary expressions were written as
>>     if condition then value-if-true else value-if-false
>> which to me reads better than the status quo, but that ship has sailed.)
>> Rob Cliffe
> 
> What about:
>    x = entries[0] except IndexError then None
> 
> The weird point with:
>    x = entries[0] except IndexError else None
> 
> is that 'else' seems to introduce a kind of double negation, where the first negation is due to 'except'. It this seems to indicate what _not_ to do in case of exception, which indeed makes no sense. 'else instead is ok in reverse order:
>    x = entries[0] else None if IndexError
> 
> However, 'then' in python normally introduces an action,

'then' in Python normally means nothing.

If Python _had_ a then keyword in it's if statement, there would have been a much more obvious syntax for the if expression, but it doesn't.

> meaning a statement or block (and I'm firmly opposed to giving unrelated meanings to keywords or signs). But in such a case, an expression context, the action is just to choose and pick a value (here for the assignment), thus finally I don't find it that bad.
> 
> d
> 
> _______________________________________________
> 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/


More information about the Python-ideas mailing list