[Python-ideas] duck typing for io write methods
Nick Coghlan
ncoghlan at gmail.com
Fri Jun 14 08:52:39 CEST 2013
On 14 June 2013 16:14, Amaury Forgeot d'Arc <amauryfa at gmail.com> wrote:
>
> 2013/6/14 Nick Coghlan <ncoghlan at gmail.com>
>>
>> On 14 June 2013 10:08, Steven D'Aprano <steve at pearwood.info> wrote:
>> > The actual behaviour, where bytes(4) => b'\x00\x00\x00\x00', I consider
>> > to
>> > be neither obvious nor especially useful. If bytes were mutable, then
>> > bytes(4) would be a useful way to initialise a block of four bytes for
>> > later
>> > modification. But they aren't, so I don't really see the point. The
>> > obvious
>> > way to get four NUL bytes is surely b'\0'*4, so it's also redundant.
>>
>> My (vague) recollection is that the intended use case was for
>> bytearray (i.e. exactly the "initialize a section of memory for
>> subsequent modification" use case you mention), and the bytes
>> constructor just supports it for consistency reasons.
>
>
> In early 3.0, bytes were mutable. So I guess this bytes(4) behavior was just
> forgotten.
Ah, I had forgotten that part of the story. Yeah, you're probably
right - it's quite likely that when it was split into bytes
(immutable) and bytearray (mutable), we simply didn't give the idea of
changing either constructor signature any thought.
Cheers,
Nick.
P.S. Trawling the python-3000 list archives would probably reveal the
exact reasoning, but this is a plausible version of history.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list