trinary operator - if then else

Nick Craig-Wood nick at craig-wood.com
Wed Apr 25 11:30:04 EDT 2007


Diez B. Roggisch <deets at nospam.web.de> wrote:
>  Michael >>
> >> Does Python 2.4 support it?
> > 
> > Not precisely, but you can *usually* get away with:
> > 
> > a and b or c
> 
>  This is really bad advice, as long as you don't explain why it "usually"
>  works (and often enough not). This for example won't work:
> 
> >>> False or '' and 0
> ''

You can use this if you want it to be bullet proof

  (a and [b] or [c])[0]

Not exactly elegant though!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list