[Python-ideas] Add an option for delimiters in bytes.hex()
Nick Coghlan
ncoghlan at gmail.com
Tue May 2 00:52:40 EDT 2017
On 2 May 2017 at 03:34, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 05/01/2017 07:04 AM, Juancarlo Añez wrote:
>>
>> On Mon, May 1, 2017 at 9:38 AM, Nick Coghlan wrote:
>>
>>> just support two
>>> keyword arguments to hex(): "delimiter" (as you suggest) and
>>> "chunk_size" (defaulting to 1, so you get per-byte chunking by
>>> default)
>>
>>
>> I'd expect "chunk_size" to mean the number of hex digits (not bytes) per
>> chunk.
>
> I was also surprised by that. Also, should Python be used on a machine
> with, say, 24-bit words then a chunk size of three makes more sense that one
> of 1.5. ;)
I came up with a possible alternative scheme on the issue tracker:
def hex(self, *, group_digits=None, delimiter=" "):
"""B.hex() -> string of hex digits
B.hex(group_digits=N) -> hex digits in groups separated by *delimeter*
Create a string of hexadecimal numbers from a bytes object::
>>> b'\xb9\x01\xef'.hex()
'b901ef'
>>> b'\xb9\x01\xef'.hex(group_digits=2)
'b9 01 ef'
"""
Advantages of this approach:
- grouping by digits generalises more obviously to other bases (e.g.
if similar arguments were ever added to the hex/oct/bin builtins)
- by using "group_digits=None" to indicate "no grouping", the default
delimiter can be a space rather than the empty string
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list