[Python-ideas] Fixing the Python 3 bytes constructor
Antoine Pitrou
solipsis at pitrou.net
Fri Mar 28 12:22:51 CET 2014
On Fri, 28 Mar 2014 20:27:33 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> One of the current annoyances with the bytes type in Python 3 is the
> way the constructor handles integers:
>
> >>> bytes(3)
> b'\x00\x00\x00'
>
> It would be far more consistent with the behaviour of other bytes
> interfaces if the result of that call was instead b'\x03'.
Which other bytes interfaces are you talking about?
> However, during a conversation today, a possible solution occurred to
> me: a "bytes.chr" class method, that served as an alternate
> constructor. That idea results in the following 3 part proposal:
>
> 1. Add "bytes.chr" such that "bytes.chr(x)" is equivalent to the PEP
> 361 defined "b'%c' % x"
You mean bytes.chr(x) is equivalent to bytes([x]). The intent is
slightly more obvious indeed, so I'd inclined to be +0, but Python
isn't really a worse language if it doesn't have that alternative
spelling.
> Anyway, what do people think? Does anyone actually *like* the way the
> bytes constructor in Python 3 currently handles integers and want to
> keep it forever?
I don't like it, but I also don't think it's enough of a nuisance to be
deprecated.
(the indexing behaviour of bytes objects is far more annoying)
Regards
Antoine.
More information about the Python-ideas
mailing list