[Python-3000] Consistency of memoryview and bytes object

Travis Oliphant oliphant.travis at ieee.org
Sun Apr 27 02:49:28 CEST 2008


Guido van Rossum wrote:
> Hm, yes this seems reasonable. Travis, what do you think of this?
> 
> On Sat, Apr 26, 2008 at 1:51 PM, Alexandre Vassalotti
> <alexandre at peadrop.com> wrote:
>>  Would it be a good idea to make memoryview indexing consistent with
>>  the behaviour of bytes object?
>>
>>   >>> memoryview(b'hello')[0]
>>   bytearray(b'h')
>>   >>> b'hello'[0]
>>   104

I'm not sure that we should rush into this.  There are reasons for the 
differences.

The idea is that an "element" of a memory-view object be a bytes object 
(either a bytearray or a bytes object depending on mutability of the 
original memoryview object --- seems like it should be a bytes object in 
this case).

Remember that an "element" of a memory-view object can have more than 
one byte depending on the format attribute.   So, I'm not sure what is 
gained by special-casing the 1-byte item except possible confusion later.

Perhaps it is useful to special-case this one, but then you lose useful 
mutability.  My feel right now is to not do the special case at all and 
actually return a memory-view object even for element access (this is 
especially needed, I think for nested formats which arise in 
memory-mapping files which provides some very handy io-related 
functionality).  Then, we should leave it to method call to extract a 
bytes object as desired.

-Travis





More information about the Python-3000 mailing list