[Python-ideas] Fast sum() for non-numbers

Andrew Barnert abarnert at yahoo.com
Fri Jul 12 06:22:00 CEST 2013


On Jul 11, 2013, at 18:34, Sergey <sergemp at mail.ru> wrote:

>> 
>> sum is not the obvious way to concatenate sequences today, and my
>> preferred way is for that to stay true. So, I'm: [...]
> 
> You're saying what IS NOT your preferred way. But I'm asking what IS
> your preferred way.

You just quoted it. My preferred way to handle this is what we already do: don't encourage people to misuse sum for concatenating sequences, encourage them to use something appropriate.

That could mean making the wording in the docs even stronger, or even explicitly rejecting sequences in sum, but I don't think the problem is anywhere near serious enough to do either of those.

That could also mean making the obvious way more obvious. In other words: move itertools.chain.from_iterable to builtins under a new name.

> Do you prefer to have a slow sum in python and
> people asking why it's slow forever? Do you see that as a best
> possible case for python?

Yes. Given that it is impossible to make sum fast for all collection types, and that it's the wrong function for concatenating collections in the first place, and that, even if it were fast, it would still be inferior to chain.from_iterable in the exact same way that 2.x map is inferior to 3.x map, I do. 

Also, It's less surprising this way, not more. Today, people only have to learn one thing: Don't use sum on collections. That's much easier than having to learn a complex mishmash like: Don't use sum on immutable collections, except for tuple, and also don't use it on some mutable collections, but it's hard to characterize exactly which, and also don't use it on things that are iterable but that you don't want to treat as sequences, and...

One of the reasons I hate PHP is that all of its rules work that way--everything does what you expect in 60% of the cases, and does something baffling in the other 40%.

Finally, I've ignored your requests for examples because in every case you've already been given examples and haven't dealt with any of them. If I want to concatenate cons lists, chain does it in linear time, your design does not; instead of answering that, you just keep arguing that you can sum a different kind of linked list in linear time. That doesn't even approach answering the objection. So what's the point of offering another one that you're just going to treat in the same way? Especially since I and others have already given you other examples and you just ignored them?


More information about the Python-ideas mailing list