[Numpy-discussion] object dtype questions

Sturla Molden sturla at molden.no
Sun Sep 6 08:33:52 EDT 2009


Mark Wendell skrev:
> 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
One option is to look up the name of the method unbound, and then use 
built-in function map.

map( cls.myMethod, a )

is similar to:

[aa.myMethod() for aa in a]

Using map avoids looking up the name 'myMethod' for each instance, it is 
done only once. map can be a lot faster or hardly faster at all, 
depending on the amount of work done by the function. The more work 
being performed, the less you benefit from map.

You can pass in variables (var3,var4) by giving additional sequences to 
map.

S.M.



More information about the NumPy-Discussion mailing list