<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">On Fri, Mar 8, 2019 at 1:52 AM Jonathan Fine <<a href="mailto:jfine2358@gmail.com">jfine2358@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I've just learnt something new. Look at<br>
<br>
    >>> from operator import iadd<br>
    >>> lst = [1, 2, 3]<br>
    >>> iadd(lst, 'hi')<br>
    [1, 2, 3, 'h', 'i']<br>
    >>> lst<br>
    [1, 2, 3, 'h', 'i']<br>
<br>
This shows that the proposals dict.flow_update and dict.__iadd__ are<br>
basically the same. (I think this is quite important for understanding<br>
the attraction of fluent programming. We ALREADY like and use it, in<br>
the form of augmented assignment of mutables.)<br></blockquote><div><br></div><div>well, no -- the fact that __iadd__ acts like it does is essentially an accident of implementation, and calling __iadd__ directly is frowned upon (and I'm not totally sure if it is guaranteed to keep working that way by the language spec). And, in fact, it DOESN'T act like flow_merge method -- as it both mutates the original object, and returns itself -- which I think is a no-no in fluent programming, yes? (certainly in functional programming)<br></div><div><span style="font-family:monospace,monospace"><br></span></div><div><span style="font-family:monospace,monospace">In [10]: list1 = [1,2,3]<br><br>In [11]: list2 = [4,5,6]<br><br>In [12]: list3 = list1.__iadd__(list2)<br><br>In [13]: list3<br>Out[13]: [1, 2, 3, 4, 5, 6]<br><br>In [14]: list1<br>Out[14]: [1, 2, 3, 4, 5, 6]<br><br>In [15]: list1 is list3<br>Out[15]: True</span><br></div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
This also shows that<br>
   combined = defaults.copy()<br>
   combined.update(options)<br>
could, if the proposal is accepted, be written as<br>
   defaults.copy().__iadd__(options)<br></blockquote><div><br></div><div>did you mean:</div><div><br></div><div><span style="font-family:monospace,monospace">combined = defaults.copy().__iadd__(opti</span><span style="font-family:monospace,monospace">ons)</span></div><div><br></div><div>because the way you wrote it, you are making a copy, mutating it, and then throwing it away...<br></div><div><br></div><div>in which case, yes it could, but it would not be recommended, and I can't see the advantage of it over:</div><div><br></div><div><span style="font-family:monospace,monospace">combined = defaults + opti</span><span style="font-family:monospace,monospace">ons</span></div><div><br></div><div>or even, if you REALLY want to use __ methods:</div><div><br></div><div><span style="font-family:monospace,monospace">combined = defaults.__add__(options)</span></div><div><br></div><div><span style="font-family:monospace,monospace"><br></span></div><div><span style="font-family:monospace,monospace">In [17]: list3 = list1.__add__(list2)<br><br>In [18]: list1<br>Out[18]: [1, 2, 3]<br><br>In [19]: list3<br>Out[19]: [1, 2, 3, 4, 5, 6]</span></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I got the idea from the withdrawn PEP (thank you, Nick Coghlan, for writing it):<br>
PEP 577 -- Augmented Assignment Expressions<br>
<a href="https://www.python.org/dev/peps/pep-0577/" rel="noreferrer" target="_blank">https://www.python.org/dev/peps/pep-0577/</a><br></blockquote><div><br></div><div>Interestingly (to me) it was withdrawn for different reasons than what I would think -- mutating and assigning at once is dangerous.</div><div><br></div><div>-CHB</div><div><br></div><div> <br></div></div>-- <br><div dir="ltr" class="gmail_signature">Christopher Barker, PhD<br><br> Python Language Consulting<br>  - Teaching<br>  - Scientific Software Development<br>  - Desktop GUI and Web Development<br>  - wxPython, numpy, scipy, Cython<br></div></div></div></div>