"Byte" type?

Scott David Daniels Scott.Daniels at Acm.Org
Thu Mar 12 13:51:04 EDT 2009


John Nagle wrote:
> Martin v. Löwis wrote:
>>>> Please don't call something dumb that you don't fully understand....
>>> ...- do you have to convert twice?
>> Depends on how you write your code. If you use the bytearray type
>> (which John didn't, despite his apparent believe that he did),
>> then no conversion additional conversion is needed.
> 
>     According to PEP 3137, there should be no distinction between
> the two for read purposes.  In 2.6, there is.  That's a bug.
If we something like the following for the bytes type in 2.7, perhaps
we could improve the 2.X behavior (and make everyone happier).

     class bytes(str):

         def __getitem__(self, index):
             # keep exceptions the same
             result = super(bytes, self).__getitem__(index)
             if isinstance(index, int):
                 # Picking out a single char
                 return ord(result)
              # otherwise, it an extraction, return a similar type.
              return bytes(result)

         def __repr__(self):
             # make a nice visible-in-debugger type
             return 'b' + super(bytes, self).__repr__()


At the very least, you can smuggle this into your programs
to check out their behavior.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list