This piece of code works fine for me:

> >>> somevar = bytes()
> >>> somevar
> ''
> >>> somevar.replace(b'', b'10')
> '10'
> >>> somevar
> ''
> >>> somevar = somevar.replace(b'', b'10')
> >>> somevar
> '10'
> >>> somevar2 = bytes(b'10'*2)
> >>> somevar2
> '1010'
> >>> somevar2 = somevar2.replace(b'01', b'57'*3)
> >>> somevar2
> '15757570'
>
> They're unmutable, but replace deals with it.
2011/5/17 Ethan Furman <et...@stoneleaf.us>

> Felipe Bastos Nunes wrote:
>
>  2011/5/17 Ethan Furman wrote:
>>
>>>
>>> In Python 3 one can say
>>>
>>> --> huh = bytes(5)
>>>
>>> Since the bytes type is actually a list of integers, I would have
>>>    expected this to have huh being a bytestring with one element -- the
>>> integer 5.  Actually, what you get is:
>>>
>>> --> huh
>>> b'\x00\x00\x00\x00\x00'
>>>
>>> or five null bytes.  Note that this is an immutable type, so you
>>> cannot go in later and say
>>>
>>> --> huh[3] = 9
>>> Traceback (most recent call last):
>>>  File "<stdin>", line 1, in <module>
>>> TypeError: 'bytes' object does not support item assignment
>>>
>>>
>>> So, out of curiosity, does anyone actually use this, um, feature?
>>>
>> >
>
>> They accept .replace(b"00", b"12") for example.
>>
>
> So they do.  Although that particular example doesn't work since b'0' is
> the integer 48...
>
> --> huh.replace(b'00',b'12')
>
> b'\x00\x00\x00\x00\x00'
>
>
> 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)
>
> ~Ethan~
>



-- 
Felipe Bastos Nunes
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to