[Python-ideas] except expression

spir denis.spir at gmail.com
Thu Feb 13 19:45:18 CET 2014


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, 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



More information about the Python-ideas mailing list