[Numpy-discussion] how to create an array of objects that are sequences?
Fri Jan 4 13:24:31 CST 2008
Just ignore this solution. It was not quite working
and I was able to get a segfault from it.
On Fri, January 4, 2008 8:58 pm, Pearu Peterson wrote:
> On Fri, January 4, 2008 8:00 pm, Pearu Peterson wrote:
>> On Fri, January 4, 2008 7:33 pm, Travis E. Oliphant wrote:
>>> Pearu Peterson wrote:
>>>> 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.
>>> So, create an empty object array and insert the entries the way you
>>> a = np.empty(1,dtype=object)
>>> a = A((1,2))
>> Meantime I was reading arrayobject.c and it seems that
>> before objects are checked for being sequences, their
>> __array_interface__ is accessed (eg in Array_FromSequence,
>> Would this provide a solution if the class A would define
>> a property __array_interface__? I just don't know what
>> the data field should be for an object.
> Ok, I found a partial solution:
> class A(tuple):
> def __repr__(self):
> return 'A(%s)' % (tuple.__repr__(self))
> def __array_interface__(self):
> import numpy
> obj = numpy.empty(1,dtype=object)
> obj = self
> return obj.__array_interface__.copy()
> Numpy-discussion mailing list
More information about the Numpy-discussion