conditional expressions (RE: Loop-and-a-half (Re: Curious assignment behaviour))

Michael Abbott michael at rcp.co.uk
Tue Oct 16 05:34:18 EDT 2001


Paul Rubin <phr-n2001d at nightsong.com> wrote in 
news:7xelo4yoj4.fsf at ruckus.brouhaha.com:

> "Anders J. Munch" <andersjm at dancontrol.dk> writes:
>> Combine that with the form mentioned (though not preferred) by Michael
>> Abbott elsewhere we get:
>> 
>>     x = (if e1: e2 else: e3)
>> 
>> No new keyword, consistent with if-statement syntax and hence more
>> intuitive to someone who knows Python but not a heap of other
>> languages as well.  And it's slightly more terse than the if-then-else
>> form, which is important because terseness is the only reason to use a
>> conditional expression in the first place. 
> 
> Hey, I like that one too.  You could even include an elif:
>   x = (if e1: v1 elif e2: v2 else: v3)

I have to say I prefer the emerging form

    	x = if e1 then v1 elif e2 then v2 else v3

The parentheses are certainly not required, and can always be added by 
those who like to use them.

I'm pretty sure that  

    	x = if e1: v1 elif e2: v2 else: v3

will work without the brackets; again, bracket enthusiasts can add them to 
taste!



More information about the Python-list mailing list