A library approach to the ternary operator
Jp Calderone
exarkun at intarweb.us
Fri Mar 21 18:03:49 EST 2003
On Fri, Mar 21, 2003 at 10:47:55PM +0000, Brandon Beck wrote:
> >My choice would be:
> >
> ># ternary module
> >def select(test, fn_iftrue, fn_iffalse):
> > if test:
> > return fn_iftrue()
> > return fn_iffalse()
> >
> >then:
> >
> >from ternary import *
> >x = select(condition, lambda:true_result, lambda:false_result)
>
>
> Another possibility along these sames lines is:
>
> # ternary module
> def select(test, iftrue, iffalse):
> if test:
> if callable(iftrue):
> return iftrue()
> else:
> return iftrue
> else:
> if callable(iffalse):
> return iffalse()
> else:
> return iffalse()
>
> then the user can use either the short circuiting or non-short
> circuiting version depending on what they want and what's more
> appropriate/readable.
>
> Both of these then work:
> x = select(condition, lambda:true_result, lambda:false_result)
> x = select(condition, true_result, false_result)
>
... until you try to use an actual callable as true_result or
false_result.
In the face of ambiguity, refuse the temptation to guess.
Jp
--
#!/bin/bash
( LIST=(~/.sigs/*.sig)
cat ${LIST[$(($RANDOM % ${#LIST[*]}))]}
echo -- $'\n' `uptime | sed -e 's/.*m//'` ) > ~/.signature
--
up 1 day, 19:59, 6 users, load average: 0.00, 0.02, 0.03
More information about the Python-list
mailing list