[Python-Dev] Which direction is UnTransform? / Unicode is different

Chris Angelico rosuav at gmail.com
Wed Nov 20 13:13:32 CET 2013


On Wed, Nov 20, 2013 at 11:03 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> I *will* get confused over which
>> direction is encoding and which is decoding. (Removing .decode()
>> from the (unicode) str type in 3 does help a lot, if I have a Python 3
>> interpreter running to check against.)
>
> It took me a long time to learn that text encodes to bytes, and bytes
> decode back to text. Using Python 3 really helped with that.

Rule of thumb: Stuff gets encoded for transmission/storage and decoded
for usage.

That covers encryption (you transmit the coded form and read the
decoded), compression (you store the tighter form and use the
expanded), Unicode (you store bytes, you work with characters), and
quite a few others. I don't know that it's an iron-clad rule (though I
can't off-hand think of a contrary example), but it's certainly an
easy way to remember a lot of the encode/decode pairs.

ChrisA


More information about the Python-Dev mailing list