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

Evan evan at 4-am.com
Sat Feb 8 18:14:25 EST 2003


Andrew Dalke wrote:
> if "spam" in s:
>   if "eggs" in s:
>     t = "vikings"
>   else:
>     t = "waitress"
> else:
>   t = "customer"
> 
> or
> 
>   t = ("vikings" if "eggs" in s else "waitress") if "spam" in s else "customer"

How about this, as I suggested elsewhere in this thread:

t = {if "spam" in s:
       {if "eggs" in s:
         "vikings",
         "waitress},
       "customer"}

or equivalently:

t = {if "spam" not in s: "customer", "eggs" in s: "vikings", "waitress"}

Cheers,

Evan @ 4-am





More information about the Python-list mailing list