Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can I use a conditional in a variable declaration?

Reply
Thread Tools

Can I use a conditional in a variable declaration?

 
 
volcs0@gmail.com
Guest
Posts: n/a
 
      03-19-2006
I've done this in Scheme, but I'm not sure I can in Python.

I want the equivalent of this:

if a == "yes":
answer = "go ahead"
else:
answer = "stop"

in this more compact form:


a = (if a == "yes": "go ahead": "stop")


is there such a form in Python? I tried playing around with lambda
expressions, but I couldn't quite get it to work right.

 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      03-19-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I want the equivalent of this:
>
> if a == "yes":
> answer = "go ahead"
> else:
> answer = "stop"
>
> in this more compact form:
>
>
> a = (if a == "yes": "go ahead": "stop")


If the value for the 'true' case can never have a boolean value of
False, you can use this form:

a = (a == "yes") and "go ahead" or "stop"

The short-circuit evaluation of 'and' and 'or' give the correct result.
This will not work correctly because the 'and' will always evaluate to
"" which is False so the last term will be evaluated and returned:

a = (a == "yes") and "" or "stop"

and IMO the extra syntax needed to fix it isn't worth the trouble; just
spell out the if / else.

Kent
 
Reply With Quote
 
 
 
 
Ben Cartwright
Guest
Posts: n/a
 
      03-19-2006
(E-Mail Removed) wrote:
> I've done this in Scheme, but I'm not sure I can in Python.
>
> I want the equivalent of this:
>
> if a == "yes":
> answer = "go ahead"
> else:
> answer = "stop"
>
> in this more compact form:
>
>
> a = (if a == "yes": "go ahead": "stop")
>
>
> is there such a form in Python? I tried playing around with lambda
> expressions, but I couldn't quite get it to work right.


There will be, in Python 2.5 (final release scheduled for August 2006):

>>> answer = "go ahead" if a=="yes" else "stop"


See:
http://mail.python.org/pipermail/pyt...er/056846.html
http://www.python.org/doc/peps/pep-0308/

--Ben

 
Reply With Quote
 
volcs0@gmail.com
Guest
Posts: n/a
 
      03-19-2006
Kent - Thanks for the quick reply. I tried the and/or trick - it does
work. But you're right - more trouble than its worth.... So for now, I
did it "the long way". It looks like (see below), this functionality
will be added in soon.

Thanks for the quick help.

-sam

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-19-2006
(E-Mail Removed) writes:
> a = (if a == "yes": "go ahead": "stop")
>
> is there such a form in Python? I tried playing around with lambda
> expressions, but I couldn't quite get it to work right.


This has been the subject of huge debate over the years. The answer
is Python doesn't currently have it, but it will be added to a coming
version: See http://www.python.org/doc/peps/pep-0308/

To do it in the most general way with lambda expressions, use (untested):

a = (lambda: iffalse_expression,
lambda: iftrue_expression)[bool(condition)]()

That makes sure that only one of the target expressions gets evaluated
(they might have side effects).

There are various more common idioms like

a = (condition and iftrue_expression) or iffalse_expression

which can go wrong and evaluate both expressions. It was a bug caused
by something like this that led to conditional expressions finally
being accepted into Python.
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      03-19-2006
On 2006-03-19, (E-Mail Removed) <(E-Mail Removed)> wrote:

> I want the equivalent of this:
>
> if a == "yes":
> answer = "go ahead"
> else:
> answer = "stop"


If that's what you want, then write that.

--
(E-Mail Removed)
Grant Edwards

 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      03-19-2006
(E-Mail Removed) wrote:

> I want the equivalent of this:
>
> if a == "yes":
> answer = "go ahead"
> else:
> answer = "stop"
>
> in this more compact form:
>
> a = (if a == "yes": "go ahead": "stop")
>
> is there such a form in Python? I tried playing around with lambda
> expressions, but I couldn't quite get it to work right.


Rather than lambda, this merits a named function. You only have to
define it once.

def mux(s, t, f):
if s:
return t
return f

def interpret(a):
answer = mux(a == "yes", "go ahead", "stop")
print answer

interpret("yes") # Prints "go ahead."
interpret("no") # Prints "stop."
 
Reply With Quote
 
Ron Adam
Guest
Posts: n/a
 
      03-19-2006
(E-Mail Removed) wrote:
> I've done this in Scheme, but I'm not sure I can in Python.
>
> I want the equivalent of this:
>
> if a == "yes":
> answer = "go ahead"
> else:
> answer = "stop"
>
> in this more compact form:
>
>
> a = (if a == "yes": "go ahead": "stop")
>
>
> is there such a form in Python? I tried playing around with lambda
> expressions, but I couldn't quite get it to work right.



I sometimes find it useful to do:


answers = {True: "go ahead", False: "stop"}

answer = answers[a == "yes"]



This is also sometimes useful when you want to alternate between two values.

values = {'a':'b', 'b':'a'} # define outside loop

while 1:
v = values[v] # alternate between 'a' and 'b'
...

There are limits to this, both the keys and the values need to be hashable.


Cheers,
Ron













 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      03-19-2006
Jeffrey Schwab wrote:
> (E-Mail Removed) wrote:
>
>> I want the equivalent of this:
>>
>> if a == "yes":
>> answer = "go ahead"
>> else:
>> answer = "stop"
>>
>> in this more compact form:
>>
>> a = (if a == "yes": "go ahead": "stop")
>>
>> is there such a form in Python? I tried playing around with lambda
>> expressions, but I couldn't quite get it to work right.

>
> Rather than lambda, this merits a named function. You only have to
> define it once.
>
> 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.

You can show this with

test = mux(False, 1/0, 1)

and

test = False and 1/0 or 1

Georg
 
Reply With Quote
 
andy
Guest
Posts: n/a
 
      03-19-2006
(E-Mail Removed) wrote:

>I've done this in Scheme, but I'm not sure I can in Python.
>
>I want the equivalent of this:
>
>if a == "yes":
> answer = "go ahead"
>else:
> answer = "stop"
>
>in this more compact form:
>
>
>a = (if a == "yes": "go ahead": "stop")
>
>
>is there such a form in Python? I tried playing around with lambda
>expressions, but I couldn't quite get it to work right.
>
>
>

How about:

a = ["stop","go ahead"][a == "yes"]

This works because:

>>> int("yes" == "yes")

1
>>> int("yes" == "no")

0

Taking into account all the previous comments - both the literal list
elements are evaluated; there is no short-cirtuiting here. If they're
just literals, it's no problem, but if they're (possibly
compute-intensive) function calls, it would matter. I find the list
evaluation easier to parse than the and/or equation, and in instances
where that would be necessary, I will use the longhand if ... else ...
structure for readability.

hth,
-andy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behavior of "use if" (a conditional "use" with the if module) David Filmer Perl Misc 2 03-29-2013 01:19 AM
Can we use break statement in conditional operator? jayapal C Programming 6 10-30-2007 12:31 PM
Conditional 'use' depending of a variable rather than a constant ? Yohan N. Leder Perl Misc 9 08-16-2006 08:44 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Passing a conditional form variable (Was: 'Trying to use images asform submit buttons') Orest Kinasevych Javascript 1 08-29-2003 04:07 AM



Advertisments