PEP 308: A PEP Writer's Experience - PRO
Samuele Pedroni
pedronis at bluewin.ch
Sun Feb 9 15:15:58 EST 2003
"Paul Rubin" <phr-n2003b at NOSPAMnightsong.com> ha scritto nel messaggio
news:7xisvt9pv9.fsf at ruckus.brouhaha.com...
> Andrew Koenig <ark at research.att.com> writes:
> > Samuele> ife = lambda box: box[0]
> > Samuele> z = ife(cond and [x] or [y])
> >
> > Samuele> if Python had had a ternary cond op, I would never have seen
> > Samuele> that <sniff>.
> >
> > You're right -- that's about the nicest workaround I've seen.
>
> I'm missing something--x and y both get evaluated in that example,
> so it's only a partial work around?
and,or short-circuit
>>> def falseStr():
... print "falseStr"
... return ''
...
>>> def trueStr():
... print "trueStr"
... return "T"
...
>>> ife=list.pop
>>> def test(cond):
... return ife(cond and [falseStr()] or [trueStr()])
...
>>> test(True)
falseStr
''
>>> test(False)
trueStr
'T'
>>> True and '' or 'T' # bad
'T'
>>>
More information about the Python-list
mailing list