Message401728
>>> (65).to_bytes()
b'A'
It seems like your proposal is mostly guided by: convert an int to a byte (bytes string of length 1). IMO this case is special enough to justify the usage of a different function.
What if people expect int.to_bytes() always return a single byte, but then get two bytes by mistake?
ch = 256
byte = ch.to_bytes()
assert len(byte) == 2 # oops
A function dedicated to create a single byte is expected to raise a ValueError for values outside the range [0; 255]. Like:
>>> struct.pack('B', 255)
b'\xff'
>>> struct.pack('B', 256)
struct.error: ubyte format requires 0 <= number <= 255 |
|
Date |
User |
Action |
Args |
2021-09-13 20:38:07 | vstinner | set | recipients:
+ vstinner, barry, rhettinger, mark.dickinson, ncoghlan, mrabarnett, petr.viktorin, ethan.furman, serhiy.storchaka, veky, brandtbucher |
2021-09-13 20:38:06 | vstinner | set | messageid: <1631565486.98.0.886822145255.issue45155@roundup.psfhosted.org> |
2021-09-13 20:38:06 | vstinner | link | issue45155 messages |
2021-09-13 20:38:06 | vstinner | create | |
|