By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Conditional Expressions in Python 2.4

P: n/a
A.M
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?

Thank you,

Alan
Jun 1 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
A.M wrote:
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

Jun 1 '06 #2

P: n/a
A.M wrote:
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]
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 2 '06 #3

P: n/a
A.M
a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]


Smart! Thanks alot.
Jun 2 '06 #4

P: n/a
A.M wrote:
Do we have the conditional expressions in Python 2.4?
bruno at modulix wrote: 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" if 1 == 1 else "No" '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
def f(): .... print "don't evaluate me"
.... return 'f'
.... def g(): .... return 'g'
.... (f(), g())[True] don't evaluate me
'g' g() if True else f()

'g'
STeVe
Jun 2 '06 #5

P: n/a
Steven Bethard a écrit :
A.M wrote:
> 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::
>>> True and 0 or [] []


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


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

(f, g)[True]()

of course.
Jun 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.