Equivalent to (a ? b : c) ?

maxm maxm at normik.dk
Mon Dec 20 10:37:57 EST 1999


From: <malraux at my-deja.com>
Subject: Equivalent to (a ? b : c) ?


> I remember seeing the Python equivalent to C's (a?b:c) inline if
> statement, but I can't find it for the life of me... can some kind soul
> jog my memory for me please?


>From my own personal Python cookbook here is:

######################################################################
## Why aren't there any unary type expression like "(Test)?trueVal:falseVal"
in Python?

Thanks to Python's dynamic typing you can easily add the C "?:" expression
to the language by defining a single function that will work with all types:

---

def ifexp(cond, trueVal, falseVal):
   if cond:
      return trueVal
   else:
      return falseVal

print "You scored %s point%s" % (score, ifexp(score == 1, "", "s"))

---

Note that you'd have to define a whole set of these functions in a
statically typed language, one for each type of the Vals.

Answer by: James Logajan <JamesL at Lugoj.Com>
######################################################################


Kind regards

    Max M


------------------------------------------------------------------------
Max M Rasmussen,   New Media Director    http://www.normik.dk   Denmark
e-mail                                   mailto:maxm at normik.dk







More information about the Python-list mailing list