
On Mon, Jun 2, 2008 at 10:06 PM, George Sakkis <george.sakkis@gmail.com> wrote:
On Mon, Jun 2, 2008 at 9:28 PM, Brandon Mintern <bmintern@gmail.com> wrote:
I thought max was implemented using += (i.e. it usually starts at 0 and uses += on each item in the iterable). If so, implementing ** _iadd_ ** would result in exactly the code you posted. I realize that I said __add__ in the first place, but __iadd__ is really what I meant.
No, it uses __add__:
$ python -c " class Set(set): __iadd__=set.__ior__ sum([Set([1]), Set([2])], Set()) " Traceback (most recent call last): File "<string>", line 3, in <module> TypeError: unsupported operand type(s) for +: 'Set' and 'Set'
You can easily see the quadratic behavior of __add__:
[snip] Ouch. Never mind my idea then. I do find that rather strange, though. It seems kind of strange to be able define an initial value but not use it as an accumulator. This means that sum on list, mutable user number classes, etc. is bound to be less efficient than it could be. Perhaps a better proposal would be "change max to use __iadd__ if available, falling back to __add__ if not", and then maybe we can revisit this idea at that time. Honestly, what's wrong with sum being defined as: def sum (iterable, start=0): acc = start for i in iteratble: acc += i return acc Even though I'm making a lot of bad proposals, I sure am learning a lot. Brandon