How can I make a function equal to 0?

Martin Manns mmanns at gmx.net
Fri Mar 21 16:43:18 EDT 2008


On Fri, 21 Mar 2008 13:12:56 -0700 (PDT)
Arnaud Delobelle <arnodel at googlemail.com> wrote:

> On Mar 21, 7:48 pm, Martin Manns <mma... at gmx.net> wrote:
> Why do you want to do that?

First thank you for your help, the callable object works.

Basically I have a lot of functions inside a numpy array,
of which most return "" and actually are derived as follows:

from numpy import *

grid_size = 1000000

class F(object):  
    def __cmp__(self, other): return 0
    def __call__(self, dummy): return ""
f = F()
myarray = array([f] * grid_size, dtype="O")

# I re-define an element in the array

myarray[34424] = lambda x: "23"

# Now, I would like to loop over all re-defined elements:

for i in itertools.izip(*nonzero(myarray)):
    print myarray[i]()

If I fill the array with 0 instead of the functions that 
return "" this works really fast.

However, I would like to be able call the content of each
cell in the array as a function.

As I said, the callable object approach works but is
about as slow as iterating through the whole array.
Perhaps I should add a __call__ function to the built-in
0? But I doubt that this helps.

Martin




More information about the Python-list mailing list