[Numpy-discussion] Non-writeable default for numpy.ndarray

Tim Hochberg tim.hochberg at ieee.org
Fri Sep 29 11:59:06 CDT 2006


Travis Oliphant wrote:
> Tim Hochberg wrote:
>   
>> Francesc Altet wrote:
>>   
>> It's not that the it's being built from ndarray, it's that the buffer 
>> that you are passing it is read only. 
>>     
> This is correct.
>   
>> In fact, I'd argue that allowing 
>> the writeable flag to be set to True in this case is actually a bug. 
>>   
>>     
> It's actually intentional.  Strings used as buffers are allowed to be 
> writeable.  This is an explicit design decision to allow pickles to load 
> without making 2 copies of the memory.   The pickled string that Python 
> creates is used as the actual memory for loaded arrays.
>
> Now, I suppose it would be possible to still allow this but be more 
> finnicky about when a string-used-as-the-memory can be set writeable 
> (i.e. we are the only reference to it).  But, this would be a fragile 
> solution as well. 
>
> My inclination is to just warn users not to use strings as buffers 
> unless they know what they are doing.  The fact that it is read-only by 
> default is enough of a guard against "accidentally" altering a string 
> you didn't want to alter.
>   
That makes sense.

Someday, when string/unicode => bytes/string, we'll be able to use the 
bytes type for this and it'll be much cleaner. But not for a while I 
imagine.

-tim






More information about the Numpy-discussion mailing list