[Python-ideas] Adding 'bytes' as alias for 'latin_1' codec.

Nick Coghlan ncoghlan at gmail.com
Thu May 26 16:55:55 CEST 2011


On Thu, May 26, 2011 at 9:17 PM, Masklinn <masklinn at masklinn.net> wrote:
> Considering the original use case, which seems to be mostly about being able to use .format, would it make more sense to be able to create "byte patterns", with formats similar to those of str.format but not identical (e.g. better control on layout would be nice, something similar to Erlang's bit syntax for putting binaries together).
>
> This would be useful to put together byte sequences from existing values to e.g. output binary formats.

We already have an entire module dedicated to the task of handling
binary formats: http://docs.python.org/py3k/library/struct

"format(n, '6d').encode('ascii')" is the right way to get the string
representation of a number as ASCII bytes. However, the programmer
needs to be aware that concatenating those bytes with an encoding that
is not ASCII compatible (such as UTF-16, UTF-32, or many of the Asian
encodings) will result in a sequence of unusable garbage. It is far,
far safer to transform everything into the text domain, work with it
there, then encode back when the manipulation is complete.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list