Can I use a conditional in a variable declaration?

Jeffrey Schwab jeff at schwabcenter.com
Sun Mar 19 10:55:46 EST 2006


Georg Brandl wrote:
> Jeffrey Schwab wrote:
>> volcs0 at gmail.com wrote:
>>
>>> I want the equivalent of this:
>>>
>>> if a == "yes":
>>>    answer = "go ahead"
>>> else:
>>>    answer = "stop"
>>>
>> def mux(s, t, f):
>>      if s:
>>          return t
>>      return f
> 
> But be aware that this is not a complete replacement for a syntactic
> construct. With that function, Python will always evaluate all three
> arguments, in contrast to the and/or-form or the Python 2.5 conditional.

Absolutely true, and I should have mentioned it.  In languages that 
allow ?: syntax, I rarely rely on its short-circuit effect, but it 
certainly is a significant difference.



More information about the Python-list mailing list