[Info] PEP 308 accepted - new conditional expressions

Discussion in 'Python' started by Reinhold Birkenfeld, Sep 30, 2005.

  1. Hi,

    after Guido's pronouncement yesterday, in one of the next versions of Python
    there will be a conditional expression with the following syntax:

    X if C else Y

    which is the same as today's

    (Y, X)[bool(C)]

    or

    C and X or Y (only if X is True)

    Reinhold
    Reinhold Birkenfeld, Sep 30, 2005
    #1
    1. Advertising

  2. Reinhold Birkenfeld wrote:

    > after Guido's pronouncement yesterday, in one of the next versions of Python
    > there will be a conditional expression with the following syntax:
    >
    > X if C else Y
    >
    > which is the same as today's
    >
    > (Y, X)[bool(C)]


    hopefully, only one of Y or X is actually evaluated ?

    > C and X or Y (only if X is True)


    hopefully, "only if X is True" isn't in fact a limitation of "X if C else Y" ?

    /... snip comment that the natural order is C, X, Y and that programmers that
    care about readable code will probably want to be extremely careful with this
    new feature .../

    </F>
    Fredrik Lundh, Sep 30, 2005
    #2
    1. Advertising

  3. [Fredrik]
    > > X if C else Y

    >
    > hopefully, only one of Y or X is actually evaluated ?


    Yes. From Guido's announcement at
    http://mail.python.org/pipermail/python-dev/2005-September/056846.html:

    > The syntax will be
    >
    > A if C else B
    >
    > This first evaluates C; if it is true, A is evaluated to give the
    > result, otherwise, B is evaluated to give the result.


    --
    Richie Hindle
    Richie Hindle, Sep 30, 2005
    #3
  4. Reinhold Birkenfeld

    Paul Rubin Guest

    Richie Hindle <> writes:
    > Yes. From Guido's announcement at
    > http://mail.python.org/pipermail/python-dev/2005-September/056846.html:
    > > The syntax will be
    > > A if C else B


    Wow, I thought this was a prank at first. Congratulations to Guido.
    I think the introduction of list and sequence comprehensions made the
    absence of a conditional construct felt even more strongly than
    before, for those of us who program in this style.
    Paul Rubin, Sep 30, 2005
    #4
  5. Fredrik Lundh wrote:
    > Reinhold Birkenfeld wrote:
    >
    >> after Guido's pronouncement yesterday, in one of the next versions of Python
    >> there will be a conditional expression with the following syntax:
    >>
    >> X if C else Y
    >>
    >> which is the same as today's
    >>
    >> (Y, X)[bool(C)]

    >
    > hopefully, only one of Y or X is actually evaluated ?


    (cough) Yes, sorry, it's not the same. The same would be

    (C and lambda:X or lambda:Y)()

    if I'm not much mistaken.

    >> C and X or Y (only if X is True)

    >
    > hopefully, "only if X is True" isn't in fact a limitation of "X if C else Y" ?
    >
    > /... snip comment that the natural order is C, X, Y and that programmers that
    > care about readable code will probably want to be extremely careful with this
    > new feature .../


    Yes, that was my comment too, but I'll not demonize it before I have used it.

    Reinhold
    Reinhold Birkenfeld, Sep 30, 2005
    #5
  6. Reinhold Birkenfeld wrote:
    > Hi,
    >
    > after Guido's pronouncement yesterday, in one of the next versions of Python
    > there will be a conditional expression with the following syntax:
    >
    > X if C else Y


    Any word on chaining?

    That is, what would happen with the following constructs:

    A if B else C if D else F
    A if B if C else D else F

    The first one is the tricky bit - it could be either

    (A if B else C) if D else F
    or
    A if B else (C if D else F)

    I'd expect the former from left-> right semantics, but reading the
    unparenthesized form, I'd see "A if B else ..." note that B is true, and
    conclude the expression evaluated to A (which would be wrong if D is false).
    Rocco Moretti, Sep 30, 2005
    #6
  7. Reinhold Birkenfeld wrote:
    > after Guido's pronouncement yesterday, in one of the next versions of Python
    > there will be a conditional expression with the following syntax:
    >
    > X if C else Y


    Hooray! After years of arguing over which syntax to use, and finally
    giving up since nobody could agree, the Benevolent Dictator did what
    only a dictator can do, and just made a damn decision already.

    Thank you, Guido! =)

    Dave
    Dave Benjamin, Sep 30, 2005
    #7
  8. Rocco Moretti wrote:
    > Reinhold Birkenfeld wrote:
    >> Hi,
    >>
    >> after Guido's pronouncement yesterday, in one of the next versions of Python
    >> there will be a conditional expression with the following syntax:
    >>
    >> X if C else Y

    >
    > Any word on chaining?
    >
    > That is, what would happen with the following constructs:
    >
    > A if B else C if D else F
    > A if B if C else D else F
    >
    > The first one is the tricky bit - it could be either
    >
    > (A if B else C) if D else F
    > or
    > A if B else (C if D else F)
    >
    > I'd expect the former from left-> right semantics, but reading the
    > unparenthesized form, I'd see "A if B else ..." note that B is true, and
    > conclude the expression evaluated to A (which would be wrong if D is false).


    It will be

    A if B else (C if D else F).

    Quote:
    """
    The priorities will be such that you can write

    x = A if C else B
    x = lambda: A if C else B
    x = A if C else B if D else E

    But you'd have to write

    if (A if C else B):
    [x for x in seq if (A if C else B)]
    A if (X if C else Y) else B
    (A if C else B) if D else E

    Note that all these are intentionally ugly. :)
    """

    Reinhold
    Reinhold Birkenfeld, Sep 30, 2005
    #8
  9. Reinhold Birkenfeld

    Carl Banks Guest

    Re: PEP 308 accepted - new conditional expressions

    Reinhold Birkenfeld wrote:
    > X if C else Y


    Oh well. Just about any conditional is better than no conditional.

    Carl Banks
    Carl Banks, Sep 30, 2005
    #9
  10. Reinhold Birkenfeld

    Ron Adam Guest

    Reinhold Birkenfeld wrote:

    > Rocco Moretti wrote:
    >
    >>Reinhold Birkenfeld wrote:
    >>
    >>>Hi,
    >>>
    >>>after Guido's pronouncement yesterday, in one of the next versions of Python
    >>>there will be a conditional expression with the following syntax:
    >>>
    >>>X if C else Y

    >>
    >>Any word on chaining?
    >>
    >>That is, what would happen with the following constructs:
    >>
    >>A if B else C if D else F
    >>A if B if C else D else F
    >>
    >>The first one is the tricky bit - it could be either
    >>
    >>(A if B else C) if D else F
    >>or
    >>A if B else (C if D else F)
    >>
    >>I'd expect the former from left-> right semantics, but reading the
    >>unparenthesized form, I'd see "A if B else ..." note that B is true, and
    >>conclude the expression evaluated to A (which would be wrong if D is false).

    >
    >
    > It will be
    >
    > A if B else (C if D else F)


    So this evaluates as if there are parentheses around each section.. Hmm?

    (A) if (B) else ( (C) if (D) else (F) )

    The first 'if' divided the expr, then each succeeding 'if' divides the
    sub expressions, etc... ?

    So ...

    A if B else C + X * Y

    Would evaluate as... ?

    A if B else (C + X * Y)


    and...

    value = X * Y + A if B else C

    would be ?

    value = (X * Y + A) if B else C

    or ?

    value = X * Y + (A if B else C)



    I think I'm going to make it a habit to put parentheses around these
    things just as if they were required.

    Cheers,
    Ron
    Ron Adam, Sep 30, 2005
    #10
  11. Dave Benjamin wrote:

    > Hooray! After years of arguing over which syntax to use, and finally
    > giving up since nobody could agree, the Benevolent Dictator did what
    > only a dictator can do, and just made a damn decision already.
    >
    > Thank you, Guido! =)


    Yes, hear hear.

    So what made him change his mind? When the debates raged over PEP 308,
    he seemed pretty dead set against it (at least by proxy) ...

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    The great artist is the simplifier.
    -- Henri Amiel
    Erik Max Francis, Sep 30, 2005
    #11
  12. Reinhold Birkenfeld

    Sam Guest

    Reinhold Birkenfeld writes:

    > Hi,
    >
    > after Guido's pronouncement yesterday, in one of the next versions of Python
    > there will be a conditional expression with the following syntax:
    >
    > X if C else Y
    >
    > which is the same as today's
    >
    > (Y, X)[bool(C)]


    What's wrong with "C ? X:Y"?

    Aside from ":" being overloaded?


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQBDPcH9x9p3GYHlUOIRAlVHAJ9zgyxICMojsZtKCimAzA8w0ILYvACfWqxv
    iWsn3baqRydINp8eYjStR18=
    =XWo0
    -----END PGP SIGNATURE-----
    Sam, Sep 30, 2005
    #12
  13. Reinhold Birkenfeld

    Jaime Wyant Guest

    On 9/30/05, Sam <> wrote:
    > Reinhold Birkenfeld writes:
    >
    > > Hi,
    > >
    > > after Guido's pronouncement yesterday, in one of the next versions of Python
    > > there will be a conditional expression with the following syntax:
    > >
    > > X if C else Y
    > >
    > > which is the same as today's
    > >
    > > (Y, X)[bool(C)]

    >
    > What's wrong with "C ? X:Y"?
    >
    > Aside from ":" being overloaded?
    >


    First thing that comes to my mind is that it is more C-ish (read
    cryptic) than pythonic (read elegant and understandable).

    jw
    Jaime Wyant, Oct 1, 2005
    #13
  14. On Fri, 30 Sep 2005 20:25:35 +0200, Reinhold Birkenfeld <> wrote:

    >Fredrik Lundh wrote:
    >> Reinhold Birkenfeld wrote:
    >>
    >>> after Guido's pronouncement yesterday, in one of the next versions of Python
    >>> there will be a conditional expression with the following syntax:
    >>>
    >>> X if C else Y
    >>>
    >>> which is the same as today's
    >>>
    >>> (Y, X)[bool(C)]

    >>
    >> hopefully, only one of Y or X is actually evaluated ?

    >
    >(cough) Yes, sorry, it's not the same. The same would be
    >
    >(C and lambda:X or lambda:Y)()
    >
    >if I'm not much mistaken.


    I think you need to parenthesize, but also note that using lambda does not
    always grab the currently-executing-scope X or Y, so I think the list container idiom
    may be better to show the semantics of the new expression:


    >>> X='this is global X'
    >>> Y='this is global Y'
    >>> C=False
    >>> def foo():

    ... C = True
    ... class K(object):
    ... X='this is K.X'
    ... Y='this is K.Y'
    ... cv = (C and (lambda:X) or (lambda:Y))()
    ... return K
    ...
    >>> def bar():

    ... C = True
    ... class K(object):
    ... X='this is K.X'
    ... Y='this is K.Y'
    ... cv = (C and [X] or [Y])[0]
    ... return K
    ...
    >>> foo().cv

    'this is global X'
    >>> bar().cv

    'this is K.X'


    >
    >>> C and X or Y (only if X is True)

    >>
    >> hopefully, "only if X is True" isn't in fact a limitation of "X if C else Y" ?
    >>
    >> /... snip comment that the natural order is C, X, Y and that programmers that
    >> care about readable code will probably want to be extremely careful with this
    >> new feature .../

    >
    >Yes, that was my comment too, but I'll not demonize it before I have used it.
    >

    Me too ;-)

    Regards,
    Bengt Richter
    Bengt Richter, Oct 1, 2005
    #14
  15. Reinhold Birkenfeld

    Terry Reedy Guest

    "Dave Benjamin" <> >
    > Hooray! After years of arguing over which syntax to use, and finally
    > giving up since nobody could agree,


    I understand that this has become the local 'politically correct' view, but
    as a participant in the discussion, I know it not true and actively
    deceptive. The community was prevented from coming to a decision. There
    was a 'primary' vote with an incumbent, 15 challengers, and several
    write-ins. But there was no runoff.

    As recorded in http://www.python.org/peps/pep-0308.html, the incumbent
    proposal, Guido's favorite from beginning to end,
    x if C else y, was tied for 1st in rejections and was only 4th in
    acceptances, being beaten by
    (if C: x else: y) (which was also first in rejections)
    C ? x : y
    if C then x else y
    Among the other proposals also, about 3/4ths had the order C x y.

    I think if nothing else, the vote showed that the community pretty strongly
    preferred the order C x y to x C y. I am pretty sure that a vote then (and
    probably now) on that specific issue would have favored the first. But
    that idea, along with any runoff among the best exemplars of each order,
    was *rejected*, along with notice that 'unofficial' votes conducted by
    community members would be ignored.

    The discussion and decision process was also inhibited by the lack of any
    ground rules as to what would be an acceptible candidate. So there was
    lots of confusion and lots of time wasted on 'favorite son' candidates that
    had no chance of winning (much as there once was on the first round of
    voting at American presidential nominating conventions). There was also,
    that I knew of, no definition of success nor a process aimed at success. I
    doubt that many actually expected the obviously preliminary vote to reach a
    definitive conclusion.

    Even in the recent py-dev discussion, Guido declined, when I specifically
    asked, to reveal his 'ballot' of alternatives he was considering. So what
    were we actually supposed to discuss? Naturally, there followed irrelevant
    noise posts on impossible alternatives and peripheral issues. And this
    became a reason for Guido to 'just decide' on his original favorite, no
    explanation offered.
    ----------

    The lesson for me is to spend much less time on Python discussion and much
    more on unfinished projects. So even if I never use the new syntax, I will
    have gained something ;-)

    Terry J. Reedy
    Terry Reedy, Oct 1, 2005
    #15
  16. Reinhold Birkenfeld

    Sam Guest

    Jaime Wyant writes:

    > On 9/30/05, Sam <> wrote:
    >> Reinhold Birkenfeld writes:
    >>
    >> > Hi,
    >> >
    >> > after Guido's pronouncement yesterday, in one of the next versions of Python
    >> > there will be a conditional expression with the following syntax:
    >> >
    >> > X if C else Y
    >> >
    >> > which is the same as today's
    >> >
    >> > (Y, X)[bool(C)]

    >>
    >> What's wrong with "C ? X:Y"?
    >>
    >> Aside from ":" being overloaded?
    >>

    >
    > First thing that comes to my mind is that it is more C-ish (read
    > cryptic) than pythonic (read elegant and understandable).


    And "foo if bar" is Perl-ish; yet, even Perl has the ? : operators.



    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQBDPgkyx9p3GYHlUOIRAtYJAJ94v5hK3gZlSGTILzXguQNs4ct2dQCfZT7V
    ZNafvlzn6LAlelGerLV9vH8=
    =xsEg
    -----END PGP SIGNATURE-----
    Sam, Oct 1, 2005
    #16
  17. Re: PEP 308 accepted - new conditional expressions

    Terry Reedy ha scritto:

    > "Dave Benjamin" <> >
    > > Hooray! After years of arguing over which syntax to use, and finally
    > > giving up since nobody could agree,

    >
    > I understand that this has become the local 'politically correct' view, but
    > as a participant in the discussion, I know it not true and actively
    > deceptive. The community was prevented from coming to a decision. There
    > was a 'primary' vote with an incumbent, 15 challengers, and several
    > write-ins. But there was no runoff.

    <snip history of PEP 308>

    FWIW, I think Terry's recollection is pretty close to the "historical
    truth".
    Guido could have decided two years ago, sparing us the PEP 308 ordalia.
    So, I am happy that at the end we will have a conditional operator, but
    I
    am not happy of how the process worked out. It was just an enormous
    waste
    of resources that could have been employed much better :-(

    Michele Simionato
    Michele Simionato, Oct 1, 2005
    #17
  18. Ron Adam wrote:

    >> It will be
    >>
    >> A if B else (C if D else F)

    >
    > So this evaluates as if there are parentheses around each section.. Hmm?
    >
    > (A) if (B) else ( (C) if (D) else (F) )
    >
    > The first 'if' divided the expr, then each succeeding 'if' divides the
    > sub expressions, etc... ?
    >
    > So ...
    >
    > A if B else C + X * Y
    >
    > Would evaluate as... ?
    >
    > A if B else (C + X * Y)
    >
    >
    > and...
    >
    > value = X * Y + A if B else C
    >
    > would be ?
    >
    > value = (X * Y + A) if B else C
    >
    > or ?
    >
    > value = X * Y + (A if B else C)
    >
    >
    >
    > I think I'm going to make it a habit to put parentheses around these
    > things just as if they were required.


    Yes, that's the best way to make it readable and understandable.

    Reinhold
    Reinhold Birkenfeld, Oct 1, 2005
    #18
  19. Erik Max Francis wrote:
    > Dave Benjamin wrote:
    >
    >> Hooray! After years of arguing over which syntax to use, and finally
    >> giving up since nobody could agree, the Benevolent Dictator did what
    >> only a dictator can do, and just made a damn decision already.
    >>
    >> Thank you, Guido! =)

    >
    > Yes, hear hear.
    >
    > So what made him change his mind? When the debates raged over PEP 308,
    > he seemed pretty dead set against it (at least by proxy) ...


    Raymond Hettinger proposed to change the behavior of and/or to return one
    of their arguments because he had been bitten by the common trap of
    C and X or Y when X is False.

    People were positive about it, but said that then a conditional would have
    to be introduced. So Guido considered it again. He says himself that the
    greatest problem last time was that no consensus over syntax was reached,
    and he solved the problem this time by deciding himself.

    Reinhold
    Reinhold Birkenfeld, Oct 1, 2005
    #19
  20. Sam wrote:
    > Jaime Wyant writes:
    >
    >> On 9/30/05, Sam <> wrote:
    >>> Reinhold Birkenfeld writes:
    >>>
    >>> > Hi,
    >>> >
    >>> > after Guido's pronouncement yesterday, in one of the next versions of Python
    >>> > there will be a conditional expression with the following syntax:
    >>> >
    >>> > X if C else Y
    >>> >
    >>> > which is the same as today's
    >>> >
    >>> > (Y, X)[bool(C)]
    >>>
    >>> What's wrong with "C ? X:Y"?
    >>>
    >>> Aside from ":" being overloaded?
    >>>

    >>
    >> First thing that comes to my mind is that it is more C-ish (read
    >> cryptic) than pythonic (read elegant and understandable).

    >
    > And "foo if bar" is Perl-ish; yet, even Perl has the ? : operators.


    Perl's "foo if bar" is very different to this one, you should know about this.

    For a conditional, syntax must be found, and the tradition of Python
    design is not to use punctuation for something that can be solved with
    keywords.

    Reinhold
    Reinhold Birkenfeld, Oct 1, 2005
    #20
    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. Raymond Hettinger

    PEP 289: Generator Expressions (ACCEPTED)

    Raymond Hettinger, Oct 28, 2003, in forum: Python
    Replies:
    0
    Views:
    264
    Raymond Hettinger
    Oct 28, 2003
  2. neblackcat
    Replies:
    8
    Views:
    355
    Mark 'Kamikaze' Hughes
    Jul 20, 2004
  3. Is PEP 237 accepted?

    , Nov 10, 2005, in forum: Python
    Replies:
    0
    Views:
    295
  4. Colin J. Williams

    Conditional expressions - PEP 308

    Colin J. Williams, Jan 30, 2007, in forum: Python
    Replies:
    4
    Views:
    324
    Ziga Seilnacht
    Jan 31, 2007
  5. Vinay Sajip

    PEP 414 has been accepted

    Vinay Sajip, Feb 27, 2012, in forum: Python
    Replies:
    0
    Views:
    173
    Vinay Sajip
    Feb 27, 2012
Loading...

Share This Page