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

Ethan Furman ethan at stoneleaf.us
Mon Sep 15 16:24:17 CEST 2014


On 09/10/2014 11:55 PM, Steven D'Aprano wrote:
> On Wed, Sep 10, 2014 at 01:54:17PM +0200, Wolfgang Maier wrote:
>
>> On 09/10/2014 12:57 PM, Steven D'Aprano wrote:
>
>>> However, I do support Terry's suggestion that bytes (and, I presume,
>>> bytearray) grow some sort of easy way of displaying the bytes in hex.
>>> The trouble is, what do we actually want?
>>>
>>> b'Abc' --> '0x416263'
>>> b'Abc' --> '\x41\x62\x63'
>>>
>>> I can see use-cases for both. After less than two minutes of thought, it
>>> seems to me that perhaps the most obvious APIs for these two different
>>> representations are:
>>>
>>> hex(b'Abc') --> '0x416263'
>>
>> This would require a change in the documented
>> (https://docs.python.org/3/library/functions.html#hex) behavior of
>> hex(), which I think is quite a big deal for a relatively special case.
>
> Any new functionality is going to require a change to the documentation.
>
> Changing hex() is no more of a big deal than adding a new method. I'd
> call it *less* of a big deal.
>
> In Python 2, hex() calls the dunder method __hex__. That has been
> removed in Python 3. Does anyone know why?

__hex__ and __oct__ were removed in favor of __index__.  __index__ returns the number as an integer (if possible to do 
so without conversion from, say, float or complex or ...).  __hex__ and __oct__ did the same, and were redundant.

--
~Ethan~


More information about the Python-ideas mailing list