[SciPy-dev] 'O' type arrays and containers as values...

Travis Oliphant oliphant at ee.byu.edu
Fri Nov 4 16:00:51 EST 2005


Fernando Perez wrote:

>Hi all,
>
>If I understand things correctly, the following should work fine:
>
>In [3]: import scipy
>
>In [4]: scipy.__core_version__
>Out[4]: '0.4.3.1433'
>
>In [5]: a=scipy.empty((2,2),'O')
>
>In [6]: a[0,0] = (1,2,3)
>  
>
The problem here is the ambiguity of the left hand side and how 
assignment is generally done.   There is no special-check for this 
case.   All that happens is that (1,2,3) gets converted to an object 
array and the elements copied over.  So, you are trying to do the 
equivalent of

a[0,0] = array((1,2,3),'O')

But, the right-hand side is converted to a length-3 object array --- 
with container types it's ambiguous as to what you really want for the 
object array. 

Then of course the length-3 array cannot be copied into the result.  
Now, one solution is to special-case the PyArrayObject assignment and 
check for single-index assigment and just copy whatever the value is 
directly over.   Of course the more special-cases, the slower all code 
gets.

This has always been a problem.  You would get a similar error in Numeric.

Suggestions welcome.

-Travis




More information about the SciPy-Dev mailing list