Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47379
| References | <20130607213239.3e39a448@bigbox.christie.dr> <CAEk9e3pbF1CUo_k6YGt-40ydVL-zgBrvsLfqhf0zim+L=SZ4yw@mail.gmail.com> <20130607231015.515b3105@bigbox.christie.dr> <kouk25$aq7$1@ger.gmane.org> |
|---|---|
| Date | 2013-06-08 07:37 -0400 |
| Subject | Re: Idiomatic Python for incrementing pairs |
| From | Jason Swails <jason.swails@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2883.1370691449.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Sat, Jun 8, 2013 at 2:47 AM, Peter Otten <__peter__@web.de> wrote: > > You can hide the complexity in a custom class: > > >>> class T(tuple): > ... def __add__(self, other): > ... return T((a+b) for a, b in zip(self, other)) > ... > >>> t = T((0, 0)) > >>> for pair in [(1, 10), (2, 20), (3, 30)]: > ... t += pair > ... > >>> t > (6, 60) > > (If you are already using numpy you can do the above with a numpy.array > instead of writing your own T.) > 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... All the best, Jason
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Idiomatic Python for incrementing pairs Jason Swails <jason.swails@gmail.com> - 2013-06-08 07:37 -0400
csiph-web