<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 3 July 2013 14:40, Andrew Barnert <span dir="ltr"><<a href="mailto:abarnert@yahoo.com" target="_blank">abarnert@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Jul 2, 2013, at 20:24, Mathias Panzenböck <<a href="mailto:grosser.meister.morti@gmx.net">grosser.meister.morti@gmx.net</a>> wrote:<br>
<br>
> Such a function is very tiny:<br>
><br>
> >>> import operator<br>
> >>> isum = lambda *args: reduce(operator.iadd,*args)<br>
><br>
> But this might be unexpected:<br>
><br>
> >>> l = []<br>
> >>> l2 = isum([[1,2,3]]*1000000, l)<br>
><br>
> l is now changed. In fact l == l2.<br>
<br>
</div>He explicitly suggested making one copy, before looping. So, this:<br>
<br>
sum([[1,2,3]]*1000000, l)<br>
<br>
Has to mean:<br>
<br>
reduce(operator.iadd, [[1,2,3]]*1000000, copy.copy(l))<br>
<br>
So, it's not quite a one-liner, and it doesn't have this problem.<br>
<div class="im"><br>
> But one could maybe include isum,<br>
> maybe just as recipe in the documentation or in itertools or somewhere.<br>
<br>
</div>This sounds like a good idea. Possibly even both versions, with and without the copy.<br>
<br>
While we're at it, I've always wished sum and reduce used the same name for their start/initial parameter. If we're going to be changing one (which I suspect we probably aren't, but just in case...), is this a good time to campaign for renaming the param?<br>
</blockquote><div><br></div><div>reduce only just survived being culled completely in the Python 3 transition, so if one was going to change it would be reduce. Our perspective is that if you're considering using reduce, you should remember you're writing Python rather than a pure functional language and switch to a loop instead. (By contrast, map and filter are nice alternatives to a generator expression if you are just applying an existing function)<br>
<br>Cheers,<br>Nick.<br></div><div><br></div></div>-- <br>Nick Coghlan | <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a> | Brisbane, Australia
</div></div>