[Python-Dev] Python 3.x and bytes

Greg Ewing greg.ewing at canterbury.ac.nz
Wed May 18 07:39:40 CEST 2011


Ethan Furman wrote:

> On the one hand we have the 'bytes are ascii data' type interface, and 
> on the other we have the 'bytes are a list of integers between 0 - 256' 
> interface.

I think the weird part is that there exists a literal for
writing a byte array as an ascii string, and furthermore
that it's the *only* kind of literal available for bytes.

Personally I think that the default literal syntax for
bytes, and also the form produced by repr(), should have
been something more neutral, such as hex, with the ascii
form available for use when it makes sense. Currently if
you want to write a bytes literal in hex, you have to
say something like

    some_var = b'\xde\xad\xbe\xef'

which is ugly and unreadable. Much nicer would be

    some_var = x'deadbeef'

As for

> --> some_other_var[3] == b'd'

there ought to be a literal for specifying an integer
using an ascii character, so you could say something like

   if some_other_var[3] == c'd':

which would be equivalent to

   if some_other_var[3] == ord(b'd')

but without the overhead of computing the value each time
at run time.

-- 
Greg


More information about the Python-Dev mailing list