Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1905,18 +1905,35 @@ Evaluation order

.. index:: pair: evaluation; order

Python evaluates expressions from left to right. Notice that while evaluating
an assignment, the right-hand side is evaluated before the left-hand side.
Python evaluates expressions from left to right (except for conditional_expression).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere else the existing text just refers to them as conditional expressions in plural and without the underscore.

I’m not sure pointing out conditional expressions as the only exception is much better. Perhaps it should be except for conditional and exponentiation expressions?.

I think this sentence confuses the usage of evaluates. Perhaps we actually mean groups expressions? Compare the use of evaluate in the next sentence, meaning finding the value of operands. But then we’d be just repeating the “Operators . . . group” sentence in from the next section. Otherwise, see Terry’s 2nd point in #65058, about evaluating the middle first.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it should be except for conditional and exponentiation expressions?

Why do you think so?

>>> class A:
...     def __add__(self, other):
...         print("Hi, from A!")
...         return 1.23
... class B:
...     def __add__(self, other):
...         print("Hi, from B!")
...         return 3.21
...         
>>> (A() + A())**(B() + B())
Hi, from A!
Hi, from B!
1.9435486122336787

@vadmium vadmium Oct 5, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry I was confused. Perhaps the “left to right except conditional” got me thinking “conditional expressions must be the opposite, right to left” and therefore we must be talking about grouping, not evaluation.

Notice that while evaluating an assignment, the right-hand side is evaluated before
the left-hand side.

Examples for left to right:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps “Example [singular] of left-to-right grouping”?


.. code-block:: python

2 + 3 * 4 / 2 - 1
7

Following is an example of right to left conditional_expression evaluation example:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of right-to-left conditional expression grouping


.. code-block:: python

'foo' if 1 else 'bar' if 0 else 'baz'
'foo'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these would be clearer if they included the interactive prompt >>>


In the following lines, expressions will be evaluated in the arithmetic order of
their suffixes::
their suffixes:

.. code-block:: python

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just keep the ::, it automatically creates a code block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A explicit code block has better emphasis compared to to the :
Additionally too keep it consistent with the examples I wrote above 1927 I assumed its worth changing line 1927 too.
If you think is not needed should I revise and push again?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only 18 files use .. code-block:: python, while 460 use ::. So, I think it's best to use ::.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing out to the inconsistencies. Since, .. code-block:: python
has better emphasis overall compared to ::
I don't mind refactoring all the 460 instances of ::
that said I would like to hear from a member of cpython team as well on the best course of action!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... just use ::. The indented block is clear enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original pr I made was for adding examples and clarity to the documentation. I will wait for a member to review and approve or comment on that before I make any more changes. Thanks for the feedback

@picnixz picnixz Oct 5, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I prefer .. code-block:: python when the snippets are long enough or if I don't want a colon to open my code-block (in this case, I however would prefer to keep a smaller diff since the changes are only cosmetics and we prefer not having cosmetic changes).

There is a difference between :: and .. code-block:: python. The first one uses the default highlighting and automatically adds a colon at the end of the sentence, namely this makes

Hello::

	...

equivalent to

Hello:

.. code-block::

   ...

See https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#literal-blocks.


expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2
expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2


.. _operator-summary:
Expand All @@ -1930,8 +1947,11 @@ Operator precedence
The following table summarizes the operator precedence in Python, from highest
precedence (most binding) to lowest precedence (least binding). Operators in
the same box have the same precedence. Unless the syntax is explicitly given,
operators are binary. Operators in the same box group left to right (except for
exponentiation and conditional expressions, which group from right to left).
operators are binary. Operators in the same box group left to right (except for
comparisons, including tests, exponentiation and conditional expressions, which
group from right to left which all have the same precedence and chain from left
to right — see section Comparisons — and exponentiation, which groups from right
to left)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exponentiation listed twice. The exceptions don’t all have the same precedence. Partially redundant with the paragraph below.


Note that comparisons, membership tests, and identity tests, all have the same
precedence and have a left-to-right chaining feature as described in the
Expand Down