[Python-Dev] Why can't I encode/decode base64 without importing a module?

Antoine Pitrou solipsis at pitrou.net
Thu Apr 25 14:57:45 CEST 2013

Le Thu, 25 Apr 2013 12:05:01 +0200,
Lennart Regebro <regebro at gmail.com> a écrit :
> > The Wikipedia page does talk about *text* and *characters* for
> > the result of base64 encoding.
> So are saying that you want the Python implementation of base64
> encoding to take 8-bit binary data in bytes format and return a
> Unicode string containing the Base64 encoded data?

I'm not wanting anything here, since that would clearly break backwards
compatibility. But I think binascii should have gone that way in Python
3, indeed. binascii.b2a_hex(), for example, would be much more
practical if it returned str, rather than bytes.

> Python 3 still views text as Unicode only.

Python 3 doesn't *view* text as unicode, it *represents* it as unicode.
That is, unicode is the character set that Python 3 is able to
represent in the canonical text type, str. If you ever encounter a
hypothetical text that uses characters outside of Unicode (obviously it
will be encoded using a non-unicode encoding :-)), then you can't
represent it as a str.

And base64 is clearly representable as unicode, since it's
representable using the ASCII character set (which is a subset of the
unicode character set).

> I can't think of
> any usecase where you would want base64 encoded data unless you intend
> to transmit it over an 8-bit channel,

I can think of many usecases where I want to *embed* base64-encoded
data in a larger text *before* encoding that text and transmitting
it over a 8-bit channel.

(GPG signatures, binary data embedded in JSON objects, etc.)



More information about the Python-Dev mailing list