[Python-ideas] Fixing the Python 3 bytes constructor

Antoine Pitrou solipsis at pitrou.net
Fri Mar 28 17:29:27 CET 2014


On Fri, 28 Mar 2014 10:28:17 -0400
Barry Warsaw <barry at python.org> wrote:
> 
> I like bytearray.fill() for this.  The first argument would be the fill count,
> but it could take an optional second argument for the byte value to fill it
> with, which would of course default to zero.  E.g.
> 
> >>> bytearray.fill(5)
> bytearray(b'\x00\x00\x00\x00\x00')
> >>> bytearray.fill(5, 97)
> bytearray(b'aaaaa')

Do we need another spelling for a very uncommon use case?

>>> bytearray([0]) * 5
bytearray(b'\x00\x00\x00\x00\x00')
>>> bytearray([97]) * 5
bytearray(b'aaaaa')

Let's keep it simple.

Regards

Antoine.




More information about the Python-ideas mailing list