3 ms·
Was thinking exactly the same. Plus you save an import. And no need to wrap it in a function since it's a short method call on a built-in. Would bytearray([97,
by sametmax 10y ago
Was thinking exactly the same. Plus you save an import. And no need to wrap it in a function since it's a short method call on a built-in. Would bytearray([97,98,99]).decode('ascii') be even faster ?
- tyingq 10y agoIt would fail when you pass it 128. He's passing in values from 0-255. "Ascii" is sort of a misnomer in his post.
- jstimpfle 10y agoIt's better to fail than to convert something that probably isn't latin1!
- tyingq 10y agoWell, it's not clear what behavior he was looking for. And not possible to compare speed without an encoding that works for 0-255.
- mark-r 10y agoThe latin1 codec doesn't fail for any values in the 0-255 range. It probably should, since there are values that don't map to a valid character. I don't know if this is deliberate and guaranteed or just an artifact of the current implementation. There should have been a 'byte' encoding that was explicitly made for 1:1 conversions.