Conditional operator in Python?

Weet Vanniks Weet.Vanniks at el_simpatico.be
Tue Sep 4 12:57:55 CEST 2001


Your counter-example seems contorted to me.
I would never use:
    whatever = cond_choose(cond, format_disk(), leave_it_as_is())
for the mere reason that I have been taught that:
    1. when there is room for confusion, people get confused
    2. arguments in a function call should NEVER have side-effects.
    3. obfuscated one-liners with lambda functions bring more confusion
than anything else
        (the alledged efficiency is still to be demonstrated as you
correctly state)
    4. The spirit of Python is : "If it ain't obvious, it ain't Python"
and contorted syntax does not help support that spirit

Weet



Fredrik Lundh wrote:

> Weet Vanniks wrote:
> > What about :
> >
> > def cond_choose(cond,first_choice,second_choice):
> >      if cond:
> >           return first_choice
> >      return second_choice
> >
> > whatever= cond_choose(x,a,b)
>
> well, compare
>
> whatever = cond_choose(cond, format_disk(), leave_it_as_is())
>
> with
>
>     if cond:
>         whatever = format_disk()
>     else:
>         whatever = leave_it_as_is()
>
> > This seems much cleaner to me than all those contorsions with
> > the syntax.
>
> of course, the best way to avoid those contorsions is to fix the
> Return and Tab keys on your keyboard, and learn how and when
> to use them...
>
> (it also helps to know that lambdas and list constructors are
> expensive, and assignments to temporary locals are cheap.
> And contrary to common belief, the Python interpreter can
> skip over linefeeds in almost no time at all...)
>
> </F>




More information about the Python-list mailing list