[Python-ideas] Fast sum() for non-numbers - why so much worries?
Ronald Oussoren
ronaldoussoren at mac.com
Wed Jul 10 16:26:47 CEST 2013
On 10 Jul, 2013, at 8:33, Paul Moore <p.f.moore at gmail.com> wrote:
>
> On 10 July 2013 07:09, Joshua Landau <joshua at landau.ws> wrote:
> I agree it's not totally backward-compatible, but AFAICT that's only
> for broken code. __iadd__ should always just be a faster, in-place
> __add__ and so this change should never cause problems in properly
> written code. That makes it anything but a semantic change. It's the
> same way people discuss the order of __hash__ calls on updates to code
> but no-one calls it a *semantics* change.
>
> It will stop working for tuples (which have no __iadd__. Or were you suggesting trying __iadd__ and falling back to __add__ (that's more complex, obviously, and I don't think I'd assume it's "trivial" extra complexity) or special-caseing tuples (that's even more complex, and doesn't solve the problem for other iimmutables)?
Both "+=" in Python and its C API equivalent (PyNumber_InPlaceAdd) perform an in place addition (__iadd__) when possible and fall back to using normal addition (__add__) when the in place method is not supported. Thus barring bugs and creative misuse of in place operators using += instead of + in sum shouldn't affect the result of sum.
Still-unconviced-about-the-usefulness-ly yours,
Ronald
More information about the Python-ideas
mailing list