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’

and if I try to convert an integer value greater than 255, I get the same 
OverflowError?

Seems good enough to me.

-Barry

> On Sep 9, 2021, at 08:53, Christopher Barker <python...@gmail.com> wrote:
> 
> I fully admit serious bikeshedding here, but:
> 
> I'm starting to think the name should be `bytes.bchr` -- it avoids any 
> confusion with the `int.to_bytes` and
> `int.from_bytes` methods,
> 
> are they so different? :-)
> 
> In [23]: x.to_bytes(1, 'little')
> Out[23]: b'A'
> 
> In [27]: int.from_bytes(b'A', 'little')
> Out[27]: 65
> 
> you can think of this as a useful specific case of the int methods.
> 
> (by the way, why do I need to specify the byte order for a 1 byte int? Yes, I 
> know, it's always a required parameter -- though that 's one reason to have 
> the special case easily available)
> 
> and is an appropriate name for the target domain (where bytes are treated as 
> characters).
> 
> Is that the target domain? Yes, it's an important use case, but certainly not 
> the only one, and frankly kind of a specialized use case actually. If you are 
> working with characters (text) in Python 3, you should be using the str type.
> 
> Using bytes for general text (even if you know the text at hand is all ASCII) 
> is not recommended. It is useful to use bytes for the specialized use case of 
> mixed text and binary data (which, by the way, I have had to do) but I don't 
> think we should say that particular use case is what bytes are  targeted for. 
> Anyone doing that should know what they are doing :-)
> 
> -CHB
> 
> --
> Christopher Barker, PhD (Chris)
> 
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@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/S3Q6NYRXHKUQLMP7WCQMCEEK3MCCGKNJ/
> Code of Conduct: http://python.org/psf/codeofconduct/

Attachment: signature.asc
Description: Message signed with OpenPGP

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@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/PUR7UCOITMMH6TZVVJA5LKRCBYS4RBMR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to