If there is a problem to solve here it's that people want to
concatenate things and reach out to the sum function because the
concatenate function doesn't exist.

But that’s not the problem here — sum IS concatenation for any type for which __add__ means concatonate.

The problem is that continuous concatenation is inefficient for many types. 

np.concatonate() exists because __add__ means something different for arrays. And it is only used for arrays, so it can be done efficiently.

But how would a builtin concatonate() function know how to concatonate arbitrary objects? 

I suppose there could be a __concat__ dunder. Which is what __add__ already is for Sequences. 

The fact is that only the type itself knows how to efficiently concatonate a lot of smaller objects. Which is why str.join() is a string method — it is about strings, and only works with strings. 

Which makes me think maybe the solution is to make join() (or call it concatonate()) a Sequence method. 

Or, since it’s problematic to add new methods to ABCs ( the name may already be used by existing custom Sequence types), go back to the __concat__ dunder, which would take an  iterable of objects to concatonate. 

Which inspires a new idea: extend the Sequence __add__ to take an optional additional parameter, which could be an iterable of objects to add up. 

I’m not sure which is more expensive— trying to pass an extra parameter to an object to see if it will except it, or checking for the existence for a dunder. But I do like that there wouldn’t be a need for a new dunder. 

What remains is a key question of what people need: a generic and efficient way to concatonate Sequences, or a way to flatten sequences. 

They are overlap, but are not the same, particularly if there is a need to flatten more than one level deep.

-CHB


The sum function refuses to sum
strings but the preferred alternative is
    text = ''.join(strings)
which is just weird looking. Why would you call a method on the empty
string? It would be much better spelt as
    text = concatenate(strings)
and that spelling could work equally well for lists, tuples, etc.

--
Oscar
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YFGGY4TDNHHEGJ2T5UED67W44SXRKNHA/
Code of Conduct: http://python.org/psf/codeofconduct/
--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython