Skip to content

Context of documentation for conditional expressions #65058

Open

Description

@terryjreedy
BPO 20859
Nosy @rhettinger, @terryjreedy, @ezio-melotti, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2014-03-06.18:48:00.756>
labels = ['type-bug', '3.9', '3.10', '3.11']
title = 'Context of documentation for conditional expressions'
updated_at = <Date 2021-06-23.11:04:06.628>
user = 'https://github.com/terryjreedy'

bugs.python.org fields:

activity = <Date 2021-06-23.11:04:06.628>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2014-03-06.18:48:00.756>
creator = 'terry.reedy'
dependencies = []
files = []
hgrepos = []
issue_num = 20859
keywords = []
message_count = 2.0
messages = ['212835', '396409']
nosy_count = 4.0
nosy_names = ['rhettinger', 'terry.reedy', 'ezio.melotti', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue20859'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Linked PRs

Activity

  1. terryjreedy commented on Mar 6, 2014

    @terryjreedy
    MemberAuthor

    The documentation for conditional expressions (c_exps)
    http://docs.python.org/3/reference/expressions.html#conditional-expressions
    and the later sections on evaluation order and operator precedence have 3 inconsistencies with each other. I believe the latter (the 'context' of the title) were not properly adjusted with the addition of the former to match the exceptional behavior of c_exps.

    1. Associativily-grouping: I believe the grammar line
      conditional_expression ::= or_test ["if" or_test "else" expression]
      makes c_exps group right to left, as in C. Something like
      conditional_expression ::= expression ["if" or_test "else" or_test]
      would have the opposite effect. The following examples posted on python-list by 'candide' illustrate and verify.
    >>> 0 if 1 else 0 if 0 else 1
    0
    >>> 0 if 1 else (0 if 0 else 1)
    0
    >>> (0 if 1 else 0) if 0 else 1
    1

    This sentence in the Operator Precedence section
    http://docs.python.org/3/reference/expressions.html#operator-precedence

    "Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons — and exponentiation, which groups from right to left)."

    should have the last part revised to

    "and exponentiation and conditional expressions, which group from right to left".

    Perhaps a sentence and example should also be added to the C-E section also.

    1. Evaluation Order: Condition expressions evaluate the middle subexpession first, making
      "Python evaluates expressions from left to right."
      http://docs.python.org/3/reference/expressions.html#evaluation-order
      wrong as is, without noting the exception of c_exps.

    I think "Except for conditional expressions, python evaluates expressions from left to right." gives too much weight to the exception.

    "Python evaluates expressions from left to right (except for conditional expressions)." is a bit more awkward, but, appropriately, makes the exception more like a footnote.

    1. Precedence: "Conditional expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations." versus the table, which lists lambda as lowest priority. Should 'except for lambda' be added to the end of the sentence? Should the top two lines of the table be reversed? I do not know which is true, or perhaps neither. I get the impression from the grammar and the discussion in PEP-308 that there are sort-of at the same level. Raymond, I add you as a PEP author particularly for this question.
  2. serhiy-storchaka commented on Jun 23, 2021

    @serhiy-storchaka
    Member

    "and exponentiation and conditional expressions, which group from right to left".

    LGTM

    "Python evaluates expressions from left to right (except for conditional expressions)."

    LGTM. It would be nice to add also examples for conditional and assignment expressions.

    Should 'except for lambda' be added to the end of the sentence?

    Yes, lambda and assignment expressions have lower priority.

  3. transferred this issue fromon Apr 10, 2022
  4. added
    docsDocumentation in the Doc dir
    3.12only security fixes
    3.13only security fixes
    and removed on Nov 23, 2023
  5. added 2 commits that reference this issue on Nov 18, 2026
  6. lul-cas commented on Nov 18, 2026

    @lul-cas
    SponsorContributor

    In #162167 the "(except for conditional expressions)" wording for "Evaluation order" was dropped after review, since it would be incomplete (comprehensions are not evaluated left to right either). Nowt he PR only documents the grouping.

    Should point 2 be considered resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13only security fixesdocsDocumentation in the Doc direasytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions