This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: precedence rules for ternary operator
Type: Stage:
Components: Documentation Versions: Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: djc, eric.smith, ezio.melotti, georg.brandl, meador.inge
Priority: normal Keywords: patch

Created on 2010-03-02 14:26 by djc, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue-8039.patch meador.inge, 2010-03-07 04:53 patch against 2.7 trunk
Messages (4)
msg100294 - (view) Author: Dirkjan Ochtman (djc) * (Python committer) Date: 2010-03-02 14:26
So http://docs.python.org/reference/expressions.html doesn't currently mention the ternary operator as far as I can see. Maybe this is trivial, but it would be nice to know where it fits into the hierarchy.

(I.e., my co-worker just came up to me asking when he needed the parentheses around the entire expression and when he didn't, and I couldn't give him a good answer.)
msg100341 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-03-03 13:25
For what it's worth, these are properly called "Conditional Expressions". See PEP 308 for the gory details, including figuring out what the precedence is. It was news to me that the allowed syntax is slightly different in 2.x and 3.x.
msg100562 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2010-03-07 04:53
It seems to me from the grammar (http://docs.python.org/reference/expressions.html#grammar-token-conditional_expression) that the precedence for conditional expressions fall in between that of 'lambda' and 'or' expressions.
msg100611 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-03-07 21:13
if-else actually was already documented, under "boolean expressions".  It was merely missing from the precendence table.

I've now given it its own section, to make it stand out a bit better.  See r78772.
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52287
2010-03-07 21:13:23georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg100611
2010-03-07 04:53:00meador.ingesetfiles: + issue-8039.patch
versions: + Python 2.7
nosy: + meador.inge

messages: + msg100562

keywords: + patch
2010-03-03 13:25:21eric.smithsetnosy: + eric.smith
messages: + msg100341
2010-03-02 14:31:42djcsetnosy: + ezio.melotti
2010-03-02 14:26:03djccreate