<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style></head><body lang=EN-US link=blue vlink="#954F72"><div class=WordSection1><p class=MsoNormal>This example makes me want “if expr as name:” (same semantics as ‘with’, and the name is always bound to the expression result regardless of truthiness), but doesn’t move me on assignment expressions.</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Cheers,</p><p class=MsoNormal>Steve</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Top-posted from my Windows phone</p><p class=MsoNormal><o:p> </o:p></p><div style='mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal style='border:none;padding:0in'><b>From: </b><a href="mailto:guido@python.org">Guido van Rossum</a><br><b>Sent: </b>Saturday, April 21, 2018 19:09<br><b>To: </b><a href="mailto:steve@pearwood.info">Steven D'Aprano</a><br><b>Cc: </b><a href="mailto:python-dev@python.org">Python-Dev</a><br><b>Subject: </b>Re: [Python-Dev] PEP 572: Assignment Expressions</p></div><p class=MsoNormal><o:p> </o:p></p><div><div><div><p class=MsoNormal>On Sat, Apr 21, 2018 at 6:13 PM, Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>> wrote:</p><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><p class=MsoNormal>On Sat, Apr 21, 2018 at 08:35:51PM +0100, Matthew Woodcraft wrote:<br><br>> Well, that's a reason to make the example a bit more realistic, then.<br>> <br>> Say:<br>> <br>> if match := re.search(pat1, text):<br>> do_something_with(match.group(0))<br>> elif match := re.search(pat2, text):<br>> do_something_else_with(match.group(0), match.group(1))<br>> elif match := re.search(pat3, text):<br>> do_some_other_things_with(match.group(0))<br>> and_also_with(match.group(1), match.group(2))<br><br>I don't think that a bunch of generic "do_something_with" functions is <br>precisely "realistic".<br><br>If I saw something like that, I'd try very hard to find a way to <br>refactor it into code like this:<br><br>for handler in handlers:<br> if handler.match(text):<br> handler.process()<br> break<br>else:<br> # handle no-match case here<br><br>where the knowledge of what to search for, where to search for it, how <br>to search for it, and what to do when found, was encapsulated in the <br>handler objects. Your tastes may vary.<br><br>But your point is well-taken that the version with binding assignment <br>(thanks Tim!) is nicer to read than the current procedural version:<br><br>match = re.search(pat1, text)<br>if match:<br> do_something_with(match.group(0))<br>else:<br> match = re.search(pat2, text)<br> if match:<br> do_something_else_with(match.group(0), match.group(1))<br> else:<br> match = = re.search(pat3, text)<br> do_some_other_things_with(match.group(0))<br> and_also_with(match.group(1), match.group(2))<br><br>I just don't think it counts as a motivating use-case distinct from the <br>single match case.</p></blockquote></div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>The version of this code found in reality is not as regular as the example quoted, and the rebuttal "but I would rewrite it with a loop" shoots a straw man. To me the if-elif-elif portion of the example is very much a separate motivation, since being able to put the assignment in the elif clause avoids runaway indentation. I've regretted not being able to use elif in this kind of situation many times, whereas in the single match case I don't find it a burden to assign the variable in a separate statement preceding the if-clause. (I guess this is a case of "flat is better than nested" -- thanks Tim! :-)<br clear=all></p></div><div><p class=MsoNormal><br>-- </p></div></div><p class=MsoNormal>--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</p><p class=MsoNormal><o:p> </o:p></p></div></body></html>