<div dir="ltr"><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jun 8, 2013 at 2:47 AM, Peter Otten <span dir="ltr"><<a href="mailto:__peter__@web.de" target="_blank">__peter__@web.de</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
You can hide the complexity in a custom class:<br>
<br>
>>> class T(tuple):<br>
... def __add__(self, other):<br>
... return T((a+b) for a, b in zip(self, other))<br>
...<br>
>>> t = T((0, 0))<br>
>>> for pair in [(1, 10), (2, 20), (3, 30)]:<br>
... t += pair<br>
...<br>
>>> t<br>
(6, 60)<br>
<br>
(If you are already using numpy you can do the above with a numpy.array<br>
instead of writing your own T.)<br></blockquote><div><br></div><div style="color:rgb(0,0,0)" class="gmail_default">I do this frequently when I want data structures that behave like vectors but don't want to impose the numpy dependency on users. (Although I usually inherit from a mutable sequence so I can override __iadd__ and __isub__). It seemed overkill for the provided example, though...</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">All the best,</div><div style="color:rgb(0,0,0)" class="gmail_default">Jason</div></div></div></div>