[Python-Dev] bytes.from_hex()

Ron Adam rrr at ronadam.com
Thu Mar 2 09:03:20 CET 2006


Josiah Carlson wrote:
> Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>>    u = unicode(b)
>>    u = unicode(b, 'utf8')
>>    b = bytes['utf8'](u)
>>    u = unicode['base64'](b)   # encoding
>>    b = bytes(u, 'base64')     # decoding
>>    u2 = unicode['piglatin'](u1)   # encoding
>>    u1 = unicode(u2, 'piglatin')   # decoding
> 
> Your provided semantics feel cumbersome and confusing to me, as compared
> with str/unicode.encode/decode() .
> 
>  - Josiah

This uses syntax to determine the direction of encoding.  It would be 
easier and clearer to just require two arguments or a tuple.

      u = unicode(b, 'encode', 'base64')
      b = bytes(u, 'decode', 'base64')

      b = bytes(u, 'encode', 'utf-8')
      u = unicode(b, 'decode', 'utf-8')

      u2 = unicode(u1, 'encode', 'piglatin')
      u1 = unicode(u2, 'decode', 'piglatin')



It looks somewhat cleaner if you combine them in a path style string.

      b = bytes(u, 'encode/utf-8')
      u = unicode(b, 'decode/utf-8')

Ron



More information about the Python-Dev mailing list