STINNER Victor <vstin...@python.org> added the comment:

>>> (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

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45155>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to