Conditional expressions (again)
Terry Reedy
tjreedy at home.com
Fri Oct 26 12:05:54 EDT 2001
compare
> order.append(
> spam if customer == michae1 else
> eggs if customer == john else
> cheese)
>
> with
>
> if customer == michael:
> side0rder = spam
> elif customer == john:
> sideOrder = eggs
> else:
> sideOrder = cheese
> order.append(sideOrder)
>
> in terms of how likely each error is to arise in testing, and how
much
> time it would take to work out what was going wrong. I find that
almost
> convincing ;-)
How about (untested)
order.append( {michael:spam, john:eggs}.get(customer,cheese) )
Terry J. Reedy
More information about the Python-list
mailing list