Conditional expressions - PEP 308

C

Colin J. Williams

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
 
P

Paddy

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.
 
C

Colin J. Williams

Paddy said:
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.
 
S

Steven Bethard

Colin said:
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
 
Z

Ziga Seilnacht

Colin said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top