Conditional Expressions in Python 2.4

R

Robert Kern

A.M said:
Do we have the conditional expressions in Python 2.4?

No.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
B

bruno at modulix

A.M said:
Hi,



I am using Python 2.4. I read the PEP 308 at:

http://www.python.org/dev/peps/pep-0308/

I tried the statement:

a= "Yes" if 1==1 else "No"

but the interpreter doesn't accept it.

Do we have the conditional expressions in Python 2.4?

No, AFAIK they'll be in for 2.5

In the meanwhile, there are (sometime trickyà ways to get the same result:

a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]
 
S

Steven Bethard

A.M said:
> Do we have the conditional expressions in Python 2.4?
No, AFAIK they'll be in for 2.5

Yep:

Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
(Intel)] on win32
'Yes'

In the meanwhile, there are (sometime trickyà ways to get the same result:

a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]


And just to give some examples where the conditional expression will
show a difference::
>>> True and 0 or [] []
>>> 0 if True else []
0
.... print "don't evaluate me"
.... return 'f'
........ return 'g'
....
don't evaluate me
'g''g'


STeVe
 
B

Bruno Desthuilliers

Steven Bethard a écrit :
A.M said:
Do we have the conditional expressions in Python 2.4?

bruno at modulix wrote:
(snip)
In the meanwhile, there are (sometime trickyà ways to get the same
result:

a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]

And just to give some examples where the conditional expression will
show a difference::
[]

Yes, this is one of the tricky part !-)
<op>
Always make sure the second term doesn't eval to False.
... print "don't evaluate me"
... return 'f'
...... return 'g'
...

Why on earth are you calling the callables *before* testing ?
Should be:

(f, g)[True]()

of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top