Fun with 'str' and 'bytes'

Terry Reedy tjreedy at udel.edu
Sun Mar 6 19:44:55 EST 2011


On 3/6/2011 4:55 PM, Nicholas Devenish wrote:
> On 04/03/2011 16:40, nn wrote:
>> As far as I know, that is pretty much it. Also see:
>>
>> http://bugs.python.org/issue3982
>
> That is a depressing bug report, and really comes across as people who
> don't use networking commenting on the requirements of people who write
> networking code.
>
> It's good to see that the idea was getting a bit more treatment last yeat.

I added the following note to that issue.
"
struct.pack, not mentioned here, is a binary bytes formatting function. 
It can do ascii bytes mixed with binary octets. It works the same in 
Python 2 and 3.

Str.bytes does two things: convert objects to strings according to the 
contents of field specifiers; interpolate the resulting strings into a 
template string according to the locations of the field specifiers. If 
desired bytes represent encoded text, then encoding computed text is the 
obvious Py3 solution.

For some mixed ascii-binary uses, struct.pack is not as elegant as a 
bytes.format might be. But I think such a method should use struct 
format codes within field specifiers to convert objects into binary 
bytes rather than text.
"
Note that struct codes include s = C char[] = Py bytes of possibly 
unspecified length copied unchanged.

-- 
Terry Jan Reedy




More information about the Python-list mailing list