Conditional expressions (again)

Steve Holden sholden at holdenweb.com
Fri Oct 26 07:53:23 EDT 2001


<gbreed at cix.compulink.co.uk> wrote ...
> Erik Max Francis wrote:
>
> > Greg Ewing wrote:
> >
> > > Write that as
> > >
> > >    x = (
> > >       b if a else
> > >       d if c else
> > >       e
> > >    )
> > >
> > > and it looks a lot clearer.
> >
> > Sure, but since conditional expressions are intended for brevity, you've
> > just made a five line nested conditional express.  Why not just use an
> > if ... elif ... else statement instead?
>
> 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 ;-)
>
if customer ==  michael:
    order.append(spam)
elif customer == john:
    order.append(eggs)
else:
    order.append(cheese)

is, of course, simpler if you don't need a record of the sideOrder. I think
"cheese" should have been "chips", by the way ;-)

we-don't-need-no-stinkin'-conditional-expressions-ly y'rs  - steve
--
http://www.holdenweb.com/









More information about the Python-list mailing list