Conditional Expressions in Python 2.4

Discussion in 'Python' started by A.M, Jun 2, 2006.

  1. A.M

    A.M Guest

    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
     
    A.M, Jun 2, 2006
    #1
    1. Advertising

  2. A.M

    Robert Kern Guest

    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
     
    Robert Kern, Jun 2, 2006
    #2
    1. Advertising

  3. 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 ''.split('@')])"
     
    bruno at modulix, Jun 2, 2006
    #3
  4. A.M

    A.M Guest


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


    Smart! Thanks alot.
     
    A.M, Jun 2, 2006
    #4
  5. 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
     
    Steven Bethard, Jun 2, 2006
    #5
  6. 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.
     
    Bruno Desthuilliers, Jun 3, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alec S.
    Replies:
    10
    Views:
    10,230
    Alec S.
    Apr 16, 2005
  2. =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

    Position of test values in conditional expressions

    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=, Jun 20, 2004, in forum: C++
    Replies:
    23
    Views:
    707
    Programmer Dude
    Jun 28, 2004
  3. neblackcat
    Replies:
    8
    Views:
    368
    Mark 'Kamikaze' Hughes
    Jul 20, 2004
  4. Reinhold Birkenfeld
    Replies:
    62
    Views:
    1,126
    Andrew Koenig
    Oct 16, 2005
  5. Colin J. Williams

    Conditional expressions - PEP 308

    Colin J. Williams, Jan 30, 2007, in forum: Python
    Replies:
    4
    Views:
    336
    Ziga Seilnacht
    Jan 31, 2007
Loading...

Share This Page