Since there was talk of if-then-else not being allowed in lambda expressions, the following is from

Discussion in 'Python' started by Casey Hawthorne, Jul 21, 2006.

  1. Since there was talk of if-then-else not being allowed in lambda
    expressions, the following is from "Dive into Python"

    The and-or conditional expression trick from page 41 of "Dive into
    Python"

    Wrap the arguments in lists and then take the first element.

    >>> a = ""
    >>> b = "second"
    >>> (1 and [a] or )[0]

    ''

    --
    Regards,
    Casey
    Casey Hawthorne, Jul 21, 2006
    #1
    1. Advertising

  2. Casey Hawthorne

    Peter Otten Guest

    Casey Hawthorne wrote:

    > Since there was talk of if-then-else not being allowed in lambda
    > expressions, the following is from "Dive into Python"
    >
    > The and-or conditional expression trick from page 41 of "Dive into
    > Python"
    >
    > Wrap the arguments in lists and then take the first element.
    >
    >>>> a = ""
    >>>> b = "second"
    >>>> (1 and [a] or )[0]

    > ''


    # python 2.5
    >>> a, b = "", 0
    >>> a if False else b

    0
    >>> a if True else b

    ''

    Time to tear out that page. Really.

    Peter
    Peter Otten, Jul 21, 2006
    #2
    1. Advertising

  3. Casey Hawthorne

    Ant Guest


    > # python 2.5
    > >>> a, b = "", 0
    > >>> a if False else b

    > 0
    > >>> a if True else b

    > ''
    >
    > Time to tear out that page. Really.


    Not quite - 2.5 hasn't been released in its final version yet, and many
    projects I should imagine will take a while to upgrade.
    Ant, Jul 21, 2006
    #3
  4. Casey Hawthorne

    Peter Otten Guest

    Ant wrote:

    >
    >> # python 2.5
    >> >>> a, b = "", 0
    >> >>> a if False else b

    >> 0
    >> >>> a if True else b

    >> ''
    >>
    >> Time to tear out that page. Really.

    >
    > Not quite - 2.5 hasn't been released in its final version yet, and many
    > projects I should imagine will take a while to upgrade.


    Ok, use

    if cond:
    value = ... # expression that must not be evaluated unless it absolutely
    # has to be
    else:
    value = ... # same thing

    which will withstand the test of time. Now tear out that page...

    Peter
    Peter Otten, Jul 21, 2006
    #4
  5. Re: Since there was talk of if-then-else not being allowed in lambdaexpressions, the following is from "Dive into Python"

    Peter Otten wrote:
    > Ant wrote:
    >
    >
    >>># python 2.5
    >>>
    >>>>>>a, b = "", 0
    >>>>>>a if False else b
    >>>
    >>>0
    >>>
    >>>>>>a if True else b
    >>>
    >>>''
    >>>
    >>>Time to tear out that page. Really.

    >>
    >>Not quite - 2.5 hasn't been released in its final version yet, and many
    >>projects I should imagine will take a while to upgrade.

    >
    >
    > Ok, use
    >
    > if cond:
    > value = ... # expression that must not be evaluated unless it absolutely
    > # has to be
    > else:
    > value = ... # same thing
    >
    > which will withstand the test of time.


    But isn't allowed in a lambda !-)



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    Bruno Desthuilliers, Jul 21, 2006
    #5
  6. Casey Hawthorne

    Peter Otten Guest

    Bruno Desthuilliers wrote:

    > But isn't allowed in a lambda !-)


    Then tear out that lambada page, too, I was tempted to say, but I will
    desist. For now...

    Peter
    Peter Otten, Jul 21, 2006
    #6
  7. On Fri, 21 Jul 2006 14:36:30 +0200, Peter Otten <>
    declaimed the following in comp.lang.python:

    > Then tear out that lambada page, too, I was tempted to say, but I will
    > desist. For now...
    >

    If Python is now doing the lambada, I don't think I want to watch...
    <G>
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jul 21, 2006
    #7
    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. Steve Ferg
    Replies:
    45
    Views:
    1,807
    James Harris
    Dec 4, 2009
  2. Replies:
    0
    Views:
    261
  3. Phrogz
    Replies:
    4
    Views:
    220
    Austin Ziegler
    Sep 6, 2006
  4. Greg Hauptmann
    Replies:
    6
    Views:
    282
    ara.t.howard
    Aug 6, 2008
  5. Replies:
    15
    Views:
    209
Loading...

Share This Page