[Python-ideas] Add a .chunks() method to sequences
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu May 4 18:20:42 EDT 2017
Victor Stinner wrote:
> I prefer str.join() approach: write a single chunks() function which
> takes a sequence, instead of modifying all sequence types around the
> world ;-)
Even if a general sequence-chunking function is thought useful,
it might be worth providing a special-purpose one as a string
method in the interests of efficiency. Splitting a string into
a sequence of characters, messing with it and then joining it
back into a string is a pretty expensive way to do things.
While most uses would probably be for short strings, I can
think of uses cases involving large ones. For example, to
format a hex dump into lines with 8 bytes per line and spaces
between the lines:
data.group(2, ' ').group(24, '\n')
And even for short strings, processing lots of them in a loop
could get expensive with the general-purpose approach.
--
Greg
More information about the Python-ideas
mailing list