[Python-ideas] Add an option for delimiters in bytes.hex()

Erik python at lucidity.plus.com
Tue May 2 21:48:03 EDT 2017

On 03/05/17 01:43, Steven D'Aprano wrote:
> On Tue, May 02, 2017 at 11:39:48PM +0100, Erik wrote:
>> On 02/05/17 12:31, Steven D'Aprano wrote:
>>> Rather than duplicate the API and logic everywhere, I suggest we add a
>>> new string method. My suggestion is str.chunk(size, delimiter=' ') and
>>> str.rchunk() with the same arguments:
> For the record, I now think the second argument should be called "sep",
> for separator, and I'm okay with Greg's suggestion we call the method
> "group".
>>> "1234ABCDEF".chunk(4)
>>> => returns "1234 ABCD EF"
> [...]
>> Why do you want to limit it to strings?
> I'm not stopping anyone from proposing a generalisation of this that
> works with other sequence types. As somebody did :-)

Who? I didn't spot that in the thread - please give a reference. Thanks.

Anyway, I know you can't stop anyone from *proposing* something like 
this, but as soon as they do you may decide to quote the recipe from 
"https://docs.python.org/3/library/functions.html#zip" and try to block 
their proposition. There are already threads on fora that do that.

That was my sticking point at the time when I implemented a general 
solution. Why bother to propose something that (although it made my code 
significantly faster) had already been blocked as being something that 
should be a python-level operation and not something to be included in a 

> String methods should return strings.

In that case, we need to fix this ASAP ;) :

 >>> 'foobarbaz'.split('o')
['f', '', 'barbaz']

Where the result is reasonably a sequence, a method should return a 
sequence (but I would agree that it should generally be a sequence of 
objects of the source type - which I think is what I effectively said: 
"Isn't something like this potentially useful for all sequences (where 
the result is a [sequence] of objects that are the same [type] as the 
source sequence)"

> That's not to argue against a generic iterator solution, but the barrier
> to use of an iterator solution is higher than just calling a method.

Knowing which sequence classes have a "chunk" method and which don't is 
a higher barrier than knowing that all sequences can be "chunked" by a 
single imported function.


More information about the Python-ideas mailing list