creating array of python objects

Faheem Mitha faheem at email.unc.edu
Mon Mar 15 12:21:41 EST 2004


On Sun, 14 Mar 2004 13:37:36 -0600, Jeff Epler <jepler at unpythonic.net> wrote:
> I don't believe that numarray supports arrays of Python objects.
> The list of Numarray types doesn't list them:
> http://stsdas.stsci.edu/numarray/numarray-0.9.html/node21.html#tab:type-specifiers
>
> This is the meaning of the "not a numeric type" message.
>
> Using numpy, you can create an array of PyObjects where each entry is a
> reference to the same cell object:
>     >>> class cell:
>     ...     def setrow(self,row):
>     ...         self.row = row
>     ...     def setcol(self,col):
>     ...         self.col = col
>     ... 
>     >>> empcell = cell()
>     >>> a = Numeric.reshape([empcell]*4, (2,2))
>     >>> a[0][0].setrow(10)
>     >>> a[1][1].row         # because they are the same object
>     10

Is a[0,0] equivalent to a[0][0]? It is a slightly more compact representation.

> If you want distinct objects, you'll have to use
>     >>> a = Numeric.reshape([cell() for i in range(4)], (2,2))

Thanks.                                                         Faheem.



More information about the Python-list mailing list