[Python-Dev] bytes type discussion
"Martin v. Löwis"
martin at v.loewis.de
Wed Feb 15 01:45:39 CET 2006
Bob Ippolito wrote:
>>Martin von Loewis's alternative for the "very controversial" set is to
>>disallow an encoding argument and (I believe) also to disallow Unicode
>>arguments. In 3.0 this would leave us with s.encode(<encoding>) as the
>>only way to convert a string (which is always unicode) to bytes. The
>>problem with this is that there's no code that works in both 2.x and
>>3.0.
>
>
> Given a base64 or hex string, how do you get a bytes object out of
> it? Currently str.decode('base64') and str.decode('hex') are good
> solutions to this... but you get a str object back.
If s is a base64 string,
bytes(s.decode("base64"))
should work. In 2.x, it returns a str, which is then copied into
bytes; in 3.x, .decode("base64") returns a byte string already (*),
for which an extra copy is made.
I would prefer to see base64.decodestring to return bytes,
though - perhaps even in 2.x already.
Regards,
Martin
(*) Interestingly enough, the "base64" encoding will work reversed
in terms of types, compared to all other encodings. Where .encode
returns bytes normally, it will return a string for base64, and
vice versa (assuming the bytes type has .decode/.encode methods).
More information about the Python-Dev
mailing list