[Python-ideas] Stop displaying elements of bytes objects as printable ASCII characters in CPython 3

Paul Moore p.f.moore at gmail.com
Wed Sep 10 16:37:03 CEST 2014


On 10 September 2014 15:24, Ian Cordasco <graffatcolmingov at gmail.com> wrote:
>>> b'Abc'.decode('hexescapes') --> '\x41\x62\x63'
>>
>>
>> This, OTOH, looks elegant (avoids a new method) and clear (no doubt about
>> the returned type) to me.
>> +1
>
> Another +0.5 for me. I think this is quite elegant and reasonable. I'm
> not sure it needs to be unicode though. Perhaps it's too early for me,
> but does turning that into a unicode string make sense?

It's easy enough to do by hand:

>>> print(''.join("\\x{:02x}".format(c) for c in b'Abc'))
\x41\x62\x63

And you get any other format you like, just by changing the format
string in there, or the string you join on:

>>> print(':'.join("{:02x}".format(c) for c in b'Abc'))
41:62:63

Not every one-liner needs to be a builtin...
Paul


More information about the Python-ideas mailing list