Proposed PEP for a Conditional Expression

thp at cs.ucr.edu thp at cs.ucr.edu
Wed Sep 19 17:02:59 EDT 2001


James_Althoff at i2.com wrote:

: Tom Payne wrote:
:>I have a similar objection to the syntax:
:>
:>        cond( <exp1>, lambda:<exp2>, lambda:<exp3> )
:>
:>Moreover it doesn't scale up to multiple tests very well:
:>
:> cond( <exp>, lambda:<exp>, lambda:cond( <exp>, lambda:<exp>, lambda:<exp>
: ) )
:>
:>or stacked/indented version thereof.

: ... which is perhaps mitigated by the fact that nested conditional
: expressions is probably not a great idea for readability in any case.

To me,

          count = 1 if by_land() else
                  2 if by_sea()  else
                  0

is clearer than

         if by_land():
                 count = 1
         elif by_sea():
                 count = 2
         else:
                 count = 0

By contrast, 

         count = cond( by_land(), lambda: 1,
                 cond( by_sea(),  lambda: 2,
                                  lambda: 0
                 ))

is indeed "not a great idea for readability."

Tom Payne



More information about the Python-list mailing list