Conditional expressions - PEP 308

Colin J. Williams cjw at sympatico.ca
Tue Jan 30 16:51:49 EST 2007


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




More information about the Python-list mailing list