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

David Eppstein eppstein at ics.uci.edu
Sat Feb 8 16:05:29 EST 2003


In article <b23qth$f16$1 at slb3.atl.mindspring.net>,
 "Andrew Dalke" <adalke at mindspring.com> wrote:

> more supportable/easier to understand/prefered?
> 
> 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"
> 
> ?

Turn it around a little and both versions become a little clearer:

"customer" if "spam" not in s else "vikings" if "eggs" in s else 
"waitress"

if "spam" not in s:
    t = "customer"
elif "eggs" in s:
    t = "vikings"
else:
    t = "waitress"

But really, the reason for the unreadability of all of these forms is 
that they make no sense -- they're built up of seemingly random strings 
with no obvious logical relation between the input and the output.
I think it makes more sense to judge this proposal by looking at real 
code that might use it, than by making up code-like pieces of poetry.

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/




More information about the Python-list mailing list