Author vstinner
Recipients barry, brandtbucher, ethan.furman, mark.dickinson, mrabarnett, ncoghlan, petr.viktorin, rhettinger, serhiy.storchaka, veky, vstinner
Date 2021-09-13.20:38:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1631565486.98.0.886822145255.issue45155@roundup.psfhosted.org>
In-reply-to
Content
>>> (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
History
Date User Action Args
2021-09-13 20:38:07vstinnersetrecipients: + vstinner, barry, rhettinger, mark.dickinson, ncoghlan, mrabarnett, petr.viktorin, ethan.furman, serhiy.storchaka, veky, brandtbucher
2021-09-13 20:38:06vstinnersetmessageid: <1631565486.98.0.886822145255.issue45155@roundup.psfhosted.org>
2021-09-13 20:38:06vstinnerlinkissue45155 messages
2021-09-13 20:38:06vstinnercreate