[Numpy-discussion] how to create an array of objects that are sequences?

Travis E. Oliphant oliphant@enthought....
Fri Jan 4 11:33:52 CST 2008

Pearu Peterson wrote:
> Hi,
> Say, one defines
> class A(tuple):
>   def __repr__(self):
>     return 'A(%s)' % (tuple.__repr__(self))
> and I'd like to create an array of A instances.
> Currently I get

The array function was designed a long time ago to inspect sequences and 
flatten them.   

Arguably, there should be more intelligence when an "object" array is 
requested, but there is ambiguity about what the "right" thing to do is. 

Thus, the current situation is that if you are creating object arrays, 
the advice is to populate it after the fact. 

So, create an empty object array and insert the entries the way you want 

a = np.empty(1,dtype=object)
a[0] = A((1,2))


More information about the Numpy-discussion mailing list