Python complaints

Gareth McCaughan Gareth.McCaughan at pobox.com
Thu Nov 25 15:41:40 EST 1999


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 ''".

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
sig under construction




More information about the Python-list mailing list