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

James P. Rutledge jrut at spamcop.net
Sat Feb 8 05:21:33 CET 2003


It *might* be possible to obtain the effects of the proposed expression
in a more general way by extending the semantics of the following
existing construct:

   ("html", "text")[is_html(msg)]

The above expression provides the string "html" or "text" depending upon
the return value of the function "is_html".

If a special case were to be recognized by the parser, that of a tuple
"literal" followed immediately by an index selector, then only the tuple
item selected by the calculation of the index would need to be actually
calculated (a form of "lazy evaluation") as in the following example:

   (calc_value0(r), calc_value1(r), calc_value2(r))[index_val(x)]

The selector "index_val(x)" would be computed first, then, depending
upon the value of the index, the appropriate "calc_value" (0, 1, or 2)
would be the only "calc_value" to be computed as the value of the
expression.

Such an approach might present difficulties in parsing, but it might
have the advantage of being more general (eg. extending to more than two
possible selections) and more intuitive.

There would be a risk of breaking some current code that depends upon
such a construction and also depends upon side-effects depending upon
executing all of the various functions "calc_value", however, I suspect
that few programmers would intentionally write such code.

-Jim





More information about the Python-list mailing list