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?
As I see it, hex() returns a hexadecimal representation of its argument as a string. That's exactly what we want in this case: we're taking an object which represents a block of integer-values, and want a human- readable hexadecimal representation. So hex() is, or ought to be, the obvious solution.
As an alternative, if there was an easy, obvious way to convert the bytes b'Abc' (or b'\x41\x62\x63') to the int 4285027 (or 0x416263), then the obvious solution would be hex(int(b'Abc')) and it would require no changes to hex(). Of course the int() built-in isn't the right way to do this.