Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Conditional Expressions in Python 2.4

Reply
Thread Tools

Conditional Expressions in Python 2.4

 
 
A.M
Guest
Posts: n/a
 
      06-01-2006
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


 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      06-01-2006
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

 
Reply With Quote
 
 
 
 
bruno at modulix
Guest
Posts: n/a
 
      06-02-2006
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 '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
A.M
Guest
Posts: n/a
 
      06-02-2006

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


Smart! Thanks alot.


 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      06-02-2006
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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      06-02-2006
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.
 
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
Conditional expressions - PEP 308 Colin J. Williams Python 4 01-31-2007 04:50 PM
[Info] PEP 308 accepted - new conditional expressions Reinhold Birkenfeld Python 62 10-16-2005 06:27 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Alternative suggestion for conditional expressions (see PEP 308) neblackcat Python 8 07-20-2004 03:24 PM
Position of test values in conditional expressions =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= C++ 23 06-28-2004 08:02 PM



Advertisments