[docs] [issue16580] Add examples to int.to_bytes and int.from_bytes
Mark Dickinson
report at bugs.python.org
Sun May 26 12:35:19 CEST 2013
Mark Dickinson added the comment:
The `from_bytes` example code looks fine to me, except that I'd probably use `enumerate` in the iteration; i.e.,
sum(b << 8*i for i, b in enumerate(little_ordered))
instead of
sum(little_ordered[i] << i*8 for i in range(len(little_ordered)))
Also, in:
if signed and little_ordered and (little_ordered[-1] & 0x80):
I wondered why you needed the `little_ordered` check. But I see that `int.from_bytes(b'', 'little', signed=True)` produces `0`, which is a little bit disappointing: I was expecting an exception. (A signed format should have a sign bit, which is impossible if the length of the byte string is 0.)
The `to_bytes` example code is missing range checks for the input. It may be clearer to simply state that in the docs, instead of modifying the example code to add the range checks.
----------
nosy: +mark.dickinson
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16580>
_______________________________________
More information about the docs
mailing list