JamesL at Lugoj.Com
Fri Nov 26 01:38:54 CET 1999
Gareth McCaughan wrote:
> James Logajan wrote:
> [I said:]
> >> - if/then/else works on *statements* (strictly, suites of
> >> statements) rather than on *expressions*, so I can't
> >> say
> >> print "You scored %s point%s" % (score, if score==1 then "" else "s")
> >> or anything like that. (Or, as some people prefer to put it,
> >> Python has no ? : operator.)
> > 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"))
> That has the wrong semantics in the presence of side effects.
> (It's fine for my toy example, but then so is "score<>1 and 's' or ''".
Fair enough. Your requirements weren't just "kind of like C's '?:'
operator," they were precisely that requirement. I'm wondering though what
great application remains unwritten because you are forced to use a
conditional statement rather than use an operator?
More information about the Python-list