encode and decode builtins

Garrett Berg googberg at gmail.com
Sun Nov 16 08:39:50 CET 2014


I made the switch to python 3 about two months ago, and I have to say I
love everything about it, *especially* the change to using only bytes and
str (no more unicode! or... everything is unicode!) As someone who works
with embedded devices, it is great to know what data I am working with.

However, there are times that I do not care what data I am working with,
and I find myself writing something like:

if isinstance(data, bytes): data = data.decode()


This is tedious and breaks the pythonic method of not caring about what
your input is. If I expect that my input can always be decoded into valid
data, then why do I have to write this?

Instead, I would like to propose to add *encode* and *decode* as builtins.
I have written simple code to demonstrate my desire:

https://gist.github.com/cloudformdesign/d8065a32cdd76d1b3230

There may be a few edge cases I am missing, which would all the more prove
my point -- we need a function like this!

Basically, if I expect my data to be a string I can just write:

data = decode(data)

​Which would accomplish two goals: explicitly stating what I expect of my
data, and doing so concisely and cleanly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141116/7097c6f7/attachment.html>


More information about the Python-list mailing list