Python 3.x and bytes

Ethan Furman ethan at stoneleaf.us
Wed May 18 12:40:40 EDT 2011


Ian Kelly wrote:
> On Tue, May 17, 2011 at 2:20 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> The big question, though, is would you do it this way:
>>
>> some_var = bytes(23).replace(b'\x00', b'a')
>>
>> or this way?
>>
>> some_var = bytes(b'a' * 23)
> 
> Actually, I would just do it this way:
> 
> some_var = b'a' * 23
> 
> That's already a bytes object.  Passing it into the constructor is redundant.

However, as I just discovered, it works well when dealing with a 
bytearray object:

some_var = bytearray(b' ' * size) # want space initialized, not null

~Ethan~



More information about the Python-list mailing list