encode and decode builtins

dieter dieter at handshake.de
Mon Nov 17 02:19:11 EST 2014


Garrett Berg <googberg at gmail.com> writes:
> ...
> 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()

Apparently, below this code, you do care that "data" contains
"str" (not "bytes") -- otherwise, you could simply drop this line.

Note, in addition, that the code above might only work if "data" actually
contains ASCII only bytes (at least in Python 2.x, it would fails
otherwise). To safely decode bytes into unicode, you need to precisely
know the encoding -- something which is not consistent with
"I do not care about what data I am working with".




More information about the Python-list mailing list