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

David Gausebeck gausebec-spam at paypal.com
Fri Feb 7 21:13:30 EST 2003


>Carel> The proposal as is looks reasonable to me, it would clean up
>Carel> those few uses of (a and b or c) that I have in my code.
>
>Would you mind showing us some concrete examples?  I think that
>some real code examples would be quite useful to the discussion.

The simple example I used in a previous thread is:

   if is_html:
      extension = "html"
   else:
      extension = "txt"
   print "Processed %s.%s." % (filename, extension)

This could currently also be implemented as

   print "Processed %s.%s." % (filename, is_html and "html" or "txt")

Per the PEP, it would be

   print "Processed %s.%s." % (filename, "html" if is_html else "txt")

The alternate version that I currently like is to use essentially the
current 'if' syntax but with a different keyword, operating on
expressions instead of statements.  Of course, the big problem with
this is that it would require a new keyword.  If the keyword is
'when', it would look like:

   print "Processed %s.%s." % (filename, when is_html: "html" else: "txt")

-Dave




More information about the Python-list mailing list