From f77c9d42c7e6f2c66816086e8bea49164a8331b6 Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Fri, 8 Feb 2019 15:53:49 -0700 Subject: [PATCH 1/8] (Docs) Add := to list of operators --- Doc/reference/lexical_analysis.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst index 7e1e17edb2d8da..c0e13b53698e6b 100644 --- a/Doc/reference/lexical_analysis.rst +++ b/Doc/reference/lexical_analysis.rst @@ -887,7 +887,7 @@ The following tokens are operators: + - * ** / // % @ - << >> & | ^ ~ + << >> & | ^ ~ := < > <= >= == != From f1aac28a2f04792969f39790300bc1e0240e57ec Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Wed, 11 Sep 2019 11:40:38 +0100 Subject: [PATCH 2/8] (Docs) Add operator precedence for := to Reference - Expressions --- Doc/reference/expressions.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index ee13c5f4c6fc23..80c6fed07db789 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -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 ` -- :keyword:`!else` | Conditional expression | From dd6d5291a7f9ad11ccf127e684a3b4b7bb099e11 Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Wed, 11 Sep 2019 13:46:43 +0100 Subject: [PATCH 3/8] (Docs) Replace FAQ for "Why can't I use an assignment expression?" --- Doc/faq/design.rst | 62 +++------------------------------------------- 1 file changed, 3 insertions(+), 59 deletions(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 47fc4d498e76a9..6e2f35e42f311b 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -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 +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 -` support the iterator protocol, so you can write simply:: - - for line in f: - ... # do something with line... +See :pep:`572` for more information. From dee90eb7b504cb9b144825f298f5efff0b4a60d7 Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Wed, 11 Sep 2019 13:47:28 +0100 Subject: [PATCH 4/8] (Docs) Remove docs TODO --- Doc/whatsnew/3.8.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 07202ea4ff08d6..b284c9a63ac6e7 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -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 -------------------------- From f5b1152110a900054eb442ded67593d4a9e54f88 Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Wed, 11 Sep 2019 15:02:09 +0100 Subject: [PATCH 5/8] (Docs) Add example of assignment expression usage in Design - FAQ --- Doc/faq/design.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 6e2f35e42f311b..b10bb428233f2f 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -151,8 +151,12 @@ to tell Python which namespace to use. Why can't I use an assignment in an expression? ----------------------------------------------- -Starting in Python 3.8, you can! There is a new syntax, `:=`, that assigns a -variable in an expression. +Starting in Python 3.8, you can! + +Assignment expressions using the walrus operator `:=` assigns a variable in an expression:: + + while chunk := fp.read(200): + print(chunk) See :pep:`572` for more information. From ba1e29f97281aefcb7c0a742fcae821910464a27 Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Wed, 11 Sep 2019 15:03:49 +0100 Subject: [PATCH 6/8] (Doc) Fix typo --- Doc/faq/design.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index b10bb428233f2f..57664033140a10 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -153,7 +153,7 @@ Why can't I use an assignment in an expression? Starting in Python 3.8, you can! -Assignment expressions using the walrus operator `:=` assigns a variable in an expression:: +Assignment expressions using the walrus operator `:=` assign a variable in an expression:: while chunk := fp.read(200): print(chunk) From 04080af9c70562746818e78986ed5f3b60eac886 Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Wed, 11 Sep 2019 15:04:17 +0100 Subject: [PATCH 7/8] (Doc) Fix line break --- Doc/faq/design.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 57664033140a10..d9fac7e9834009 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -153,7 +153,8 @@ Why can't I use an assignment in an expression? Starting in Python 3.8, you can! -Assignment expressions using the walrus operator `:=` assign a variable in an expression:: +Assignment expressions using the walrus operator `:=` assign a variable in an +expression:: while chunk := fp.read(200): print(chunk) From 98eab2d52e7aa75a3fee114ab250c76018d9f655 Mon Sep 17 00:00:00 2001 From: Emily Morehouse Date: Wed, 11 Sep 2019 15:05:29 +0100 Subject: [PATCH 8/8] (Doc) Fix indentation of code block (all occurrence use 3 spaces instead of 4) --- Doc/faq/design.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index d9fac7e9834009..75cd20fb71ddf1 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -156,8 +156,8 @@ Starting in Python 3.8, you can! Assignment expressions using the walrus operator `:=` assign a variable in an expression:: - while chunk := fp.read(200): - print(chunk) + while chunk := fp.read(200): + print(chunk) See :pep:`572` for more information.