[Python-ideas] Python Convert

Stephen J. Turnbull stephen at xemacs.org
Sun Jul 14 09:20:01 CEST 2013


 > On Jul 11, 2013, at 19:01, Daniel Rode <dth4h95 at gmail.com>
 > wrote:
 >> Since Python3, the python creators removed a lot of encodings
 >> from the str.encode() method. They did it because they weren't sure
 >> how to implement the feature in Python3.

No, they specifically decided not to implement codecs that are not
directional (in the sense that they convert str to str or bytes to
bytes or both).

 >> I have an idea, add a built in method called "convert".

Only the name is new; the idea has been suggested several times.
However, the API proposed has usually been symmetric and polymorphic
(that is, either bytes-to-bytes or str-to-str).  It's arguable (and
I've argued it) that base encoding should be bytes-to-str, but
pragmatically base encodings are used mostly for content transfer
encodings in wire protocols, and in the relatively rare and
comparatively low-throughput cases where they're displayed to people,
there's no real cost to decoding from ASCII to Unicode (str),
especially since PEP 393.

Since special-case methods already exist and are well known (not to
forget easily Googled), there's little benefit to merely providing an
bunch of aliases and a registry.  So we want to reserve this
opportunity for an API that helps users to avoid double-encoding and
things like that.

Andrew Barnert writes:

 > it's just a str, which doesn't have an encoding. (Under the covers,
 > of course, it's actually stored as ASCII, UCS2, or UTF-32...)

Actually, 8-bit str is stored as ISO-8859-1.


More information about the Python-ideas mailing list