[Numpy-discussion] object dtype questions

Ernest Adrogué eadrogue@gmx....
Sun Sep 6 06:32:52 CDT 2009


 5/09/09 @ 11:22 (-0600), thus spake Mark Wendell:
> For example:
> 
> Say that C is a simple python class with a couple attributes and methods.
> 
> a = np.empty( (5,5), dtype=object)
> for i in range(5):
>      for j in range(5):
>           a[i,j] = C(var1,var2)
> 
> First question: is there a quicker way than above to create unique
> instances of C for each element of a?

You achieve the same with
a=np.array([C(var1, var2) for i in range(25)], dtype=object).reshape((5,5))
but it takes about the same time in my computer.

> for i in range(5):
>      for j in range(5):
>           a[i,j].myMethod(var3,var4)
>           print a[i,j].attribute1
> 
> Again, is there a quicker way than above to call myMethod or access attribute1?

I think you can use a ufunc:

def foo(x):
    x.myMethod(var3,var4)
	print x.attribute1

ufoo = np.frompyfunc(foo, 1, 0)
ufoo(a)

Don't know if it is any faster though.

-- 
Ernest


More information about the NumPy-Discussion mailing list