Hum, it seems like this is a confusion between converting a whole
bytes *string* to/from an integer, and converting a single *character*
to/from an integer.
I propose to rename PEP 467 method bytes.fromint(n) to =>
bytes.fromchar(n) <= to convert an integer to a single *character*: it
fails if n is not in the [0; 255] range. "char" comes from
"character", as "bchr()" means "bytes character".
For C programmers, the usage of the "char" type is common for a single
*character*. The char type is not treated as an integer, but part of a
character string. All string functions take "char*" type (strcpy,
printf, etc.). Converting an integer to a "char" in C: "int x = 1;
char ch = (char)x;".
I suggest to *not* add a builtin function bchr(), it's not common
enough to justify to add it: it's trivial to create you own bchr()
function:
bchr = bytes.fromchar
By the way, it's a little unfortunate that int methods have an
underscore in their name (int.to_bytes, int.bit_length,
int.as_integer_ratio), whereas bytes methods have no undersore in
their name (bytes.removeprefix, bytes.islower). I guess that we should
follow the trend of existing methods: so no underscore for
bytes/bytearray methods.
Victor
On Wed, Sep 8, 2021 at 7:06 PM Brandt Bucher
Steven D'Aprano wrote:
To me, it sounds like should be the opposite of int.from_bytes.
int.from_bytes(b'Hello world', 'little') 121404708502361365413651784 >>> bytes.from_int(121404708502361365413651784, 'little') # should return b'Hello world' If that's not the API being suggested, that's going to be confusing.
I'm a bit lost here... why are we convinced at all that we need a new way to do this? Hasn't this functionality already existed for years?
x = int.from_bytes(b"*", "little") x 42 x.to_bytes(1, "little") b'*'
Brandt _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/FMG5K4BO... Code of Conduct: http://python.org/psf/codeofconduct/
-- Night gathers, and now my watch begins. It shall not end until my death.