[Python-ideas] Fast sum() for non-numbers - why so much worries?

Joshua Landau joshua at landau.ws
Thu Jul 11 05:29:54 CEST 2013


On 11 July 2013 04:05, Joshua Landau <joshua at landau.ws> wrote:
> On 11 July 2013 03:36, Steven D'Aprano <steve at pearwood.info> wrote:
>> E.g. I have a DSL where = reassigns to a data structure, += appends to an
>> existing one, and + is not defined at all. You can say "x += value" but not
>> "x = x + value". It makes sense in context. As I said, I am prepared to
>> consider that the right answer to this is "well don't call sum on your data
>> structure then", but it is a change in behaviour, not just an optimization.
>
> That is... really quite a good argument. I think I may have to think
> on that final point, but you've probably just about won it. Why didn't
> you just say this from the start?

This was so close to winning me over, but think about this:

1) __iadd__ is always, for working classes, an inplace __add__. The
fact that some classes miss out one, the other or both is irrelevant
to this.

2) The new sum requires __add__ for step 1. Hence you *couldn't* sum
the DSLs. I think you would have to write a class which cheats for the
first step, but I'm not sure that that is any better than broken code. I can
at least assure you there are exactly 0 instances of this in the wild as of
now.

3) Things that have __add__ but not __iadd__ experience no behaviour
change, so the inverse of the DSLs don't change anything either.


More information about the Python-ideas mailing list