For review: PEP 308 - If-then-else expression

Bengt Richter bokr at oz.net
Mon Feb 10 04:31:42 EST 2003


On Mon, 10 Feb 2003 04:25:22 +0100, "Samuele Pedroni" <pedronis at bluewin.ch> wrote:

>
>"Paul Rubin" <http://phr.cx@NOSPAM.invalid> ha scritto nel messaggio
>news:7xznp49639.fsf at ruckus.brouhaha.com...
>> "Samuele Pedroni" <pedronis at bluewin.ch> writes:
>> > > I'm not worrieed about you; I'm worried about someone else reading
>> > > your code who might not realize that (cond and expr1 or expr2)
>> > > doesn't always work, and might use it in a case where it fails.
>> >
>> > There are ~100 instances of that in the std lib code, see Tim Peters
>> > post or the std lib directly, is for all to see.
>>
>> I see that as evidence that a conditional expression is really needed,
>> if the std lib is so full of kludgy workarounds for its absence.
>
>they are not kludgy if one is a bot <wink>
>
>I still prefer
>
>log.info(ok and "success!" or "failure.")
>print "auto colorizing turned", self.allow_colorizing and "on" or "off"
>
>over
>
>log.info(if ok: "success!" else: "failure")
>
>print "auto colorizing turned", if self.allow_colorizing: "on" else: "off"
>-or-
>print "auto colorizing turned", (if self.allow_colorizing: "on" else: "off")
>
>e.g. : seem spurious in an expression
>
OTOH,

    log.info(ok ? "failure.", "success!" )
    print "auto colorizing turned", (self.allow_colorizing ? "off", "on")

is pretty clear IMO, once you get used to the order, which I would put that
way so as to reserve
    x ?? s0, s1, s2
to have an effect as if implemented by
    ((lambda:s0),(lambda:s1),(lambda:s2))[int(x)]()
correpsonding to
    x ? s0, s1
as if implemented by
    ((lambda:s0),(lambda:s1))[bool(x)]()

Regards,
Bengt Richter




More information about the Python-list mailing list