From numpy.array docs, the object you are passing must satisfy:
An array, any object that displays the interface of an array, an object, the __array__ method returns an array or any (nested) sequence.
In fact, you are passing a list of foo objects, so this list does not expose the array interface and does not have an array method. This leaves only whether it is an embedded sequence. To be a nested sequence, your foo objects should probably be iterable. They are? ( emulating python container types )
Not sure if this is better, but you could probably do:
numpy.array([numpy.array(x) for x in [foo_1, ..., foo_n]])
Here is an example of Foo as you described. It prints the expected ndarray (no exceptions). Hope you can use it as an example:
import numpy as np class Foo(object): def __init__(self): self.arr = np.array([[1, 2, 3], [4, 5, 6], [7,8,9]], np.int32) def __array__(self): return self.arr def __iter__(self): for elem in self.arr: yield elem def __len__(self): return len(self.arr) def __getitem__(self, key): return self.arr[key] def main(): foos = [Foo() for i in range(10)] print np.array(foos) if __name__ == '__main__': main()
source share