What am I missing?

The integers between 0 and 255 map directly to a particular byte value.

But any other integer could be expressed as a wide variety of multiple byte combinations. 

The proposal here covers byte-order, but what about 16 vs 32 vs 64 bits? Unsigned vs signed? 

I thought that’s what the struct module is for.

There is the byte representation of Python’s bignum, but is that consistent across platforms and implementations? 
(Micropytjon, PyPy, IronPython, Jython) 

And even if so, is it useful? 

NOTE: my objection to “bchr”, whether as a builtin or not is not the functionality, it’s the name. Equating a byte with a character is a legacy of C ( and Python 2” — in Python 3, they are completely distinct concepts. Yes, that is serious bike-shedding :-)

-CHB 



On Wed, Sep 8, 2021 at 10:16 AM Barry Scott <barry@barrys-emacs.org> wrote:

On 8 Sep 2021, at 06:39, Steven D'Aprano <steve@pearwood.info> wrote:

On Tue, Sep 07, 2021 at 08:09:33PM -0700, Barry Warsaw wrote:

I think Nick is on board with bytes.fromint() and no bchr(), and my
sense of the sentiment here is that this would be an acceptable
resolution for most folks.  Ethan, can you reconsider?

I haven't been completely keeping up with the entire thread, so
apologies if this has already been covered. I assume that the idea is
that bytes.fromint should return a single byte, equivalent to chr()
returning a single character.

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'

:>>> int.from_bytes(b'\x00\x00\x00\x01', byteorder='big')
1
:>>> bytes.from_int(1)
would return b'\x01'? Without a length it cannot return b'\x00\x00\x00\x01'

Barry

_______________________________________________
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/TTFJ4VP5PCR557VHEH5LPSWAPNPE4QQU/
Code of Conduct: http://python.org/psf/codeofconduct/
--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython