Conditional operator in Python?

Donn Cave donn at u.washington.edu
Tue Sep 4 12:47:52 EDT 2001


Quoth Weet Vanniks <Weet.Vanniks at el_simpatico.be>:
| 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

/F is notorious for his elliptical replies, but not for contorted
syntax, and I think the former is the problem here.  The way I read
it, the usage he was promoting was the most obvious possible,
"if cond ..."

I might add, one of the reasons it's so much more obvious than the
pseudolisp alternatives is that the "if" is clearly directed at the
"cond" variable, whereas in the post that someone forged your name
to that suggested "whatever= cond_choose(x,a,b)", it's not so clear
where the condition is.

	Donn Cave, donn at u.washington.edu

----------------------------------------
| 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