restriction on sum: intentional bug?

Carl Banks pavlovevidence at
Sat Oct 17 01:53:40 CEST 2009

On Oct 16, 12:40 pm, Tim Chase <python.l... at> wrote:
> Then I'm fine with sum() being smart enough to recognize this
> horrid case and do the "right" thing by returning ''.join()
> instead.

You don't want Python to get into this business.  Trust me.  Just
don't go there.

If you want sum to call ''.join transparently, then "".join would have
to produce identical results to what sum() would have produced in all
cases.  That does not happen.  If an object within the list defines
both __str__ and __add__ methods, then "".join will call __str__,
whereas sum would call __add__, leading to potentially different
results.  Therefore, transparently substituting a call to "".join is
not an option.

It'd be better to just remove the special case.

Carl Banks

More information about the Python-list mailing list