ternary operator

Dan Bishop danb_83 at yahoo.com
Wed Feb 5 16:30:02 EST 2003


gausebec-spam at paypal.com (David Gausebeck) wrote in message news:<3e404255.1172145764 at news.cis.dfn.de>...
> I've recently started using python from a primarily C/C++ background,
> and one of the annoyances that I ran into right away was the lack of a
> ternary ?: operator.
> 
> After looking around a little, I found a number of discussions by
> others who have had the same problem.  There are a few workarounds for
> it, but none are very good.
> 
... 
> 1) Is the lack of a ternary operator widely considered a (minor)
>    shortcoming of python, or is it just a few holdouts from C/C++ who
>    care?
> 2) Is there any better workaround than the ones I've listed above?
> 
> Finally, a thought on the solution I'd like to see...  I agree that
> the C syntax of a ? b : c wouldn't really work nicely in python, but
> perhaps a unary ? operator acting on a list/tuple would.  Instead of
> "a ? b : c", python could support
> "?(a, b, c)".
> 
> The main disadvantage I can see to the syntax at this point is that it
> wouldn't support short-circuiting.

I don't see the need to add a special operator for
non-short-circuiting conditionals.  It would be to sufficent to make a
function like this standard:

def cond(selector, valTrue, valFalse):
   if selector:
      return valTrue
   return valFalse




More information about the Python-list mailing list