[Python-ideas] The non-obvious nature of str.join (was Re: sum(...) limitation)

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Aug 11 22:34:38 CEST 2014


Getting back to the topic of this thread ...

On Sun, Aug 10, 2014 at 8:33 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> I'd be interested in seeing a concrete proposal for a "concat" builtin
> that accepted a "sep" keyword only argument.


I don't think concat() is the same operation as sep.join().  I view concat
as an operation that is naturally defined on a pair of sequences and can be
naturally (via reduce) extended to apply to an iterable of sequences:

z = concat(x, y) <=> len(z) = len(x) + len(y), z[i] = x[i] if i < len(x),
z[i] = y[i - len(x)] otherwise.

(Note that the definition works for any abstract sequence.)

I find it odd that although CPython defines concatenation as a part of the
sequence protocol at the implementation level, the abstract base class
Sequence does not.  This omission may present an opportunity to design
Sequence.__concat__ and builtin concat() so that concatenation of iterables
of sequences can be implemented efficiently in concrete types.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140811/6f072ced/attachment.html>


More information about the Python-ideas mailing list