Conditional expressions - PEP 308

Discussion in 'Python' started by Colin J. Williams, Jan 30, 2007.

  1. It would be helpful if the rules of the game were spelled out more clearly.

    The conditional expression is defined as X if C else Y.
    We don't know the precedence of the "if" operator. From the little test
    below, it seem to have a lower precedence than "or".

    Thus, it is desirable for the user to put the conditional expression in
    parentheses.

    Colin W.

    # condExpr.py
    # PEP 308 defines a conditional expression as X if C else Y
    # but we don't know exactly what X is supposed to be.
    # It doesn't seem to be spelled out in the syntax.

    def main():
    names= ['abc', 'def', '_ghi', 'jkl', '_mno', 'pqrs']
    res= ''
    for w in names:
    res= res + w if w[0] != '_' else ''
    z= 1
    print 'res1:', res

    res= ''
    for w in names:
    res= res + (w if w[0] != '_' else '')
    z= 1
    print 'res2:', res

    if __name__ == '__main__':
    main()

    Result:
    [Dbg]>>>
    res1: pqrs
    res2: abcdefjklpqrs
     
    Colin J. Williams, Jan 30, 2007
    #1
    1. Advertising

  2. Colin J. Williams

    Paddy Guest

    On Jan 30, 9:51 pm, "Colin J. Williams" <> wrote:
    > It would be helpful if the rules of the game were spelled out more clearly.
    >
    > The conditional expression is defined as X if C else Y.
    > We don't know the precedence of the "if" operator. From the little test
    > below, it seem to have a lower precedence than "or".
    >
    > Thus, it is desirable for the user to put the conditional expression in
    > parentheses.
    >
    > Colin W.
    >
    > # condExpr.py
    > # PEP 308 defines a conditional expression as X if C else Y
    > # but we don't know exactly what X is supposed to be.
    > # It doesn't seem to be spelled out in the syntax.
    >
    > def main():
    > names= ['abc', 'def', '_ghi', 'jkl', '_mno', 'pqrs']
    > res= ''
    > for w in names:
    > res= res + w if w[0] != '_' else ''
    > z= 1
    > print 'res1:', res
    >
    > res= ''
    > for w in names:
    > res= res + (w if w[0] != '_' else '')
    > z= 1
    > print 'res2:', res
    >
    > if __name__ == '__main__':
    > main()
    >
    > Result:
    > [Dbg]>>>
    > res1: pqrs
    > res2: abcdefjklpqrs


    But to give them credit though, in Whats new in Python 2.5: PEP 308,
    they do mention that as a matter of style you should parenthesise the
    if-expression, and the example given consistes of just a simple
    assignment of the if-expr to a name.
    - Paddy.
     
    Paddy, Jan 31, 2007
    #2
    1. Advertising

  3. Paddy wrote:
    > On Jan 30, 9:51 pm, "Colin J. Williams" <> wrote:
    >> It would be helpful if the rules of the game were spelled out more clearly.
    >>
    >> The conditional expression is defined as X if C else Y.
    >> We don't know the precedence of the "if" operator. From the little test
    >> below, it seem to have a lower precedence than "or".
    >>
    >> Thus, it is desirable for the user to put the conditional expression in
    >> parentheses.
    >>
    >> Colin W.
    >>
    >> # condExpr.py
    >> # PEP 308 defines a conditional expression as X if C else Y
    >> # but we don't know exactly what X is supposed to be.
    >> # It doesn't seem to be spelled out in the syntax.
    >>
    >> def main():
    >> names= ['abc', 'def', '_ghi', 'jkl', '_mno', 'pqrs']
    >> res= ''
    >> for w in names:
    >> res= res + w if w[0] != '_' else ''
    >> z= 1
    >> print 'res1:', res
    >>
    >> res= ''
    >> for w in names:
    >> res= res + (w if w[0] != '_' else '')
    >> z= 1
    >> print 'res2:', res
    >>
    >> if __name__ == '__main__':
    >> main()
    >>
    >> Result:
    >> [Dbg]>>>
    >> res1: pqrs
    >> res2: abcdefjklpqrs

    >
    > But to give them credit though, in Whats new in Python 2.5: PEP 308,
    > they do mention that as a matter of style you should parenthesise the
    > if-expression, and the example given consistes of just a simple
    > assignment of the if-expr to a name.
    > - Paddy.
    >

    Yes, I agree. The ternary operator is a step forward.

    I was trying to make the point that the parentheses are necessary if X
    is more than a simple value. It's a pity that one finds this out by
    experiment rather than definition.

    Colin W.
     
    Colin J. Williams, Jan 31, 2007
    #3
  4. Colin J. Williams wrote:
    > It would be helpful if the rules of the game were spelled out more clearly.
    >
    > The conditional expression is defined as X if C else Y.
    > We don't know the precedence of the "if" operator. From the little test
    > below, it seem to have a lower precedence than "or".
    >
    > Thus, it is desirable for the user to put the conditional expression in
    > parentheses.


    Could you submit a documentation patch?

    http://sourceforge.net/tracker/?group_id=5470&atid=105470

    It doesn't need to be in LaTeX. Plain text is fine. Just indicate in
    what document you think text should be added.

    STeVe
     
    Steven Bethard, Jan 31, 2007
    #4
  5. Colin J. Williams wrote:
    > It would be helpful if the rules of the game were spelled out more clearly.
    >
    > The conditional expression is defined as X if C else Y.
    > We don't know the precedence of the "if" operator. From the little test
    > below, it seem to have a lower precedence than "or".


    The rules are specified in the Python Reference Manual:
    http://docs.python.org/ref/Booleans.html

    Ziga
     
    Ziga Seilnacht, Jan 31, 2007
    #5
    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. Christoph Becker-Freyseng

    PEP for new modules (I read PEP 2)

    Christoph Becker-Freyseng, Jan 15, 2004, in forum: Python
    Replies:
    3
    Views:
    390
    Gerrit Holl
    Jan 16, 2004
  2. neblackcat
    Replies:
    8
    Views:
    371
    Mark 'Kamikaze' Hughes
    Jul 20, 2004
  3. Reinhold Birkenfeld
    Replies:
    62
    Views:
    1,132
    Andrew Koenig
    Oct 16, 2005
  4. Lie
    Replies:
    25
    Views:
    772
    Dafydd Hughes
    Dec 18, 2007
  5. kj
    Replies:
    8
    Views:
    338
    Steven D'Aprano
    Aug 8, 2009
Loading...

Share This Page