[Python-ideas] ('blue', 'red', 'orange' if something, 'green')

Westley Martínez anikom15 at gmail.com
Mon Apr 25 04:03:59 CEST 2011


On Mon, Apr 25, 2011 at 01:18:17AM +0200, Sturla Molden wrote:
> Den 23.04.2011 21:36, skrev Westley Martínez:
> >No. This is a horrible idea.
> 
> Do you think so?  ;-)
> 
> The most "beautiful" thing with this syntax is this:
> 
> After typing
> 
>     foo = 'orange' if cond
> 
> or
> 
>     foo = 'orange' if cond else pass
> 
> we don't know if accessing foo will raise a NameError or
> not (not without checking cond that is). And we all know
> what an uncaught exception might do (cf. Ariane 5).
> 
> 
> Sturla
> 
> 

Right, the point of the else part of if/else statement is so we can
guarantee that foo is defined.



More information about the Python-ideas mailing list