[Python-ideas] .from and .to instead of .encode and .decode

Chris Barker chris.barker at noaa.gov
Mon Mar 16 17:41:05 CET 2015


On Mon, Mar 16, 2015 at 6:09 AM, anatoly techtonik <techtonik at gmail.com>
wrote:

> db.get(query).from('utf-8') gives me a clear indication that a person
> expects
> that db contents is in 'utf-8'.
>
> db.get(query).decode('utf-8') gives me a feeling that query result should
> be
> decoded from whatever format it is in to 'utf-8'.
>

In teaching this, yes, newbies do get confused about what "decode" vs.
"encode" mean. The fact that py2 strings have BOTH methods does confuse the
heck out of it! -- but that problem is solved in py3. And in this case
"from" might be a bit more obvious to a newbie. (though only might -- hard
to tell without usability testing...)

But "decode" and "encode" are widely used terms that clearly specify what's
going on -- I think that even with teh "from", it's clear to you because
you already understand a bit about the concept of unicode and the
distiction between a string object and encoded bytes. And once you get
that, then you kind know what "encode" and "decode" mean.

Hopefully, folks will get that "decoded to utf-8" is meaningless...

I suppose we could have used "decode_from" and "encode_to" in the first
place to be really clear, but I don't think it's worth making that change
now.

-CHB











> So if Python supported .from and .to, the part about encoding wouldn't feel
> like math.
> _______________________________________________
> 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/
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150316/07bed17f/attachment.html>


More information about the Python-ideas mailing list