[Python-ideas] .from and .to instead of .encode and .decode
Andrew Barnert
abarnert at yahoo.com
Sat Mar 7 14:41:30 CET 2015
On Mar 5, 2015, at 5:40 AM, anatoly techtonik <techtonik at gmail.com> wrote:
>
> Hi,
>
> While looking at the code like:
>
> 'os': sysinfo['os'].decode('utf-8'),
> 'hostname': sysinfo['hostname'].decode('utf-8'),
>
> I can't really read if the result will be unicode or binary string in
> utf-8. It would be more convenient for readability to have these
> instead:
>
> bytes.from(encoding) -> unicode
> unicode.to(encoding) -> bytes
>
> .encode/.decode are confusing, because it Python 2 it was:
>
> str.encode(encoding) -> str
> str.decode(encoding) -> str
Except for a handful of confusing cases like hex and rot13, str.decode always returned unicode, and unicode.encode always returned str.
However, str.encode _did_ return str--which it did by first decoding the str with sys.getdefaultencoding() so it had something to encode, which often failed, giving those wonderful error messages about being unable to decode ASCII when you asked it to encode UTF-8. It's little wonder that so many novices resorted to throwing in extra random calls to encode and decode and the str constructor until it seemed to work for one example, and then threw their hands up in despair when it didn't work for the next example input.
Python 3 fixed that, simply by getting rid of the Unicode decode and bytes encode methods (which were occasionally useful for codecs like hex and rot13, but far more often just confusing). So now you can only encode Unicode to bytes, and decode bytes to Unicode, and you're the first person I've seen who found the Python 3 API at all confusing. I suspect that's because you didn't realize you were confused by Python 2, and carried that confusion over to 3, and somehow didn't run into any problems for the first few years?
> with no encoding info attached.
> --
> anatoly t.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas
mailing list