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

Bengt Richter bokr at oz.net
Tue Feb 11 01:39:02 EST 2003


On Tue, 11 Feb 2003 00:04:11 +1300, Paul Foley <see at below.invalid> wrote:

>On 10 Feb 2003 09:31:42 GMT, Bengt Richter wrote:
>
>>     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
>
>That'd confuse the hell out of C programmers, though :-)
>
>
>I like
>
>   log.info([ok -> "success!"; True -> "failure."])
>   print "auto colorizing turned", [self.allow_colorizing -> "on";
>                                    True -> "off"]
>
>(of course "True" could be any true expression)
>
>+ it reads right-to-left
>
>+ the "test evaluates True" expression comes first
>
>+ it's easy to read
>
>+ it generalizes to multiple test/result pairs
>
>+ it doesn't use an ugly mix of some symbol (like "?") and a word ("else")
>
>+ it doesn't use comma (you can write [x -> a,b,c; y -> x,y,z] to get
>  one or the other tuple)
>
>+ it's the original conditional expression syntax proposed by John
>  McCarthy :-)
>
>- it uses [] for a non-list, but Python already uses other characters
>  for several different things, and that doesn't seem too awful, to me.
>  [Don't want to have to write foo((...)) to use it in functions]
>
>* it could be an error to "fall off the end" or could return None in
>  that case.  If it's going to be an error, a trailing ";" could be
>  used to imply `True->None'.
>
>+ you can easily program Emacs to turn "->" into "→" (Unicode U+2192
>  RIGHTWARDS ARROW) for display, which looks better :-)
>
Apologies, Paul (Foley!). I took this post and tweaked the idea and misattributed
the source to Paul Moore, I now realize. D'oh. I knew you were the lisp guy,
somehow I just transposed the last names *<;-/

BTW, the tweak is to drop the brackets and substitute "><" in place of ';'
as in:
    log.info(ok -> "success!" >< True -> "failure.")
    print "auto colorizing turned", self.allow_colorizing -> "on" >< True -> "off"

then also allowing a trailing ">< True" to be dropped so it looks like:
    log.info(ok -> "success!" -> "failure.")
    print "auto colorizing turned", self.allow_colorizing -> "on" -> "off"

Now also extended with Holger Krekel's idea of catching exceptions:

    cond1 -> val1 >< cond2 -> val2 >< c3->v3 >!< E1 -> ev1 >!< E2 -> ev2

I.e., if E1 or E2 is raised during processing of the first part, the exception
is caught and the associated value becomes the whole expression value.

Regards,
Bengt Richter




More information about the Python-list mailing list