
On 9/9/21 12:12 PM, Barry Warsaw wrote:
On Sep 9, 2021, at 10:56, Ethan Furman wrote:
On 9/9/21 9:37 AM, Barry Warsaw wrote:
While I think int.to_bytes() is pretty obscure (I knew about it, forgot about it, and learned about it again!) I’m not so sure it’s any less obscure than a proposed bytes.fromint().
So why don’t we just relax int.to_bytes()’s signature to include natural default values:
int.to_bytes(length=1, byteorder=sys.byteorder, *, signed=False)
Then I ought to be able to just do
>>> (65).to_bytes() b’A’
That seems so much worse than
>>> bchr(65) b'A'
;-)
Maybe, but given that you can *already* do the equivalent of bchr() with:
(65).to_bytes(1, sys.byteorder) b'A'
it seems like a small stretch to make that more usable, and that would outweigh adding a difficult to understand new builtin. TOOWTDI.
FWIW, bchr doesn't feel (much) like a new built-in, but more like a swap from unichr. At any rate, I know the built-in is not going to happen. int.to_bytes() doesn't feel like the One Obvious Way to me, and it certainly doesn't do much for readability in the bytearray case. Instead of `.fromord()` or `.fromint()` (or `int.to_bytes()`), my new favorite name, guaranteed not to change for at least the rest the day, is: bytes.chr() bytearray.chr() - this gets rid of the superflous b in bchr (not needed as the method is on bytes/bytearray) - has a nice symmetry with both the Python 3 chr(), and the answer to "where did the Python 2 chr() go?" question -- ~Ethan~