[Python-ideas] .from and .to instead of .encode and .decode
Terry Reedy
tjreedy at udel.edu
Sun Mar 8 19:31:49 CET 2015
On 3/8/2015 9:07 AM, Giampaolo Rodola' wrote:
> On Sat, Mar 7, 2015 at 2:42 PM, Luciano Ramalho
> [TIP]
> ====
> If you need a memory aid to distinguish `.decode()` from `.encode()`,
> convince yourself that a Unicode `str` contains "human" text, while
> byte sequences can be cryptic machine core dumps. Therefore, it makes
> sense that we *decode* `bytes` to `str` to get human readable text,
> and we *encode* text to `bytes` for storage or transmission.
> ====
>
>
> This is a great advice (and yes, I also often get confused by the two).
Unless a bytes object simply represents a sequence of counts in
range(256), which in unusual, then the bytes are an encoding of some
other information, which in Python is embodied in an object.
Struct.pack (en)codes python ints to bytes. Here the coding issue is
big versus little endian. Struct.unpack decodes bytes back to int
objects. PIL can decode (unpack and deserialize) bytes in various image
encodings to an image object and can likewise encode an image object to
bytes in various formats. The common theme is that non-bytes objects
are encoded to bytes for transmission and storage and decoded back to
non-bytes objects.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list