Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-35224: Additional documentation for Assignment Expressions #15935

Merged
merged 8 commits into from Sep 11, 2019
@@ -151,66 +151,10 @@ to tell Python which namespace to use.
Why can't I use an assignment in an expression?
-----------------------------------------------

Many people used to C or Perl complain that they want to use this C idiom:
Starting in Python 3.8, you can! There is a new syntax, `:=`, that assigns a

This comment has been minimized.

Copy link
@matrixise

matrixise Sep 11, 2019

Member

@emilyemorehouse
Maybe with this sentence?

Starting in Python 3.8, you can use the walrus operator `:=` that assigns a variable in an expression::

    while chunk := fp.read(200):
        print(chunk)

This comment has been minimized.

Copy link
@emilyemorehouse

emilyemorehouse Sep 11, 2019

Author Member

Thanks! I used your example and added something similar.

variable in an expression.

.. code-block:: c

while (line = readline(f)) {
// do something with line
}

where in Python you're forced to write this::

while True:
line = f.readline()
if not line:
break
... # do something with line

The reason for not allowing assignment in Python expressions is a common,
hard-to-find bug in those other languages, caused by this construct:

.. code-block:: c

if (x = 0) {
// error handling
}
else {
// code that only works for nonzero x
}

The error is a simple typo: ``x = 0``, which assigns 0 to the variable ``x``,
was written while the comparison ``x == 0`` is certainly what was intended.

Many alternatives have been proposed. Most are hacks that save some typing but
use arbitrary or cryptic syntax or keywords, and fail the simple criterion for
language change proposals: it should intuitively suggest the proper meaning to a
human reader who has not yet been introduced to the construct.

An interesting phenomenon is that most experienced Python programmers recognize
the ``while True`` idiom and don't seem to be missing the assignment in
expression construct much; it's only newcomers who express a strong desire to
add this to the language.

There's an alternative way of spelling this that seems attractive but is
generally less robust than the "while True" solution::

line = f.readline()
while line:
... # do something with line...
line = f.readline()

The problem with this is that if you change your mind about exactly how you get
the next line (e.g. you want to change it into ``sys.stdin.readline()``) you
have to remember to change two places in your program -- the second occurrence
is hidden at the bottom of the loop.

The best approach is to use iterators, making it possible to loop through
objects using the ``for`` statement. For example, :term:`file objects
<file object>` support the iterator protocol, so you can write simply::

for line in f:
... # do something with line...
See :pep:`572` for more information.



@@ -1785,6 +1785,8 @@ precedence and have a left-to-right chaining feature as described in the
+-----------------------------------------------+-------------------------------------+
| Operator | Description |
+===============================================+=====================================+
| ``:=`` | Assignment expression |
+-----------------------------------------------+-------------------------------------+
| :keyword:`lambda` | Lambda expression |
+-----------------------------------------------+-------------------------------------+
| :keyword:`if <if_expr>` -- :keyword:`!else` | Conditional expression |
@@ -887,7 +887,7 @@ The following tokens are operators:


+ - * ** / // % @
<< >> & | ^ ~
<< >> & | ^ ~ :=
< > <= >= == !=


@@ -122,8 +122,6 @@ See :pep:`572` for a full description.

(Contributed by Emily Morehouse in :issue:`35224`.)

.. TODO: Emily will sprint on docs at PyCon US 2019.

Positional-only parameters
--------------------------
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.