How can I make a function equal to 0?

M

Martin Manns

Hi,

Is there a way to create a function that is equal to 0?
I try to redefine __cmp__ but I am pretty stuck.

Something like:
True

Thanks in advance

Martin
 
M

Martin Manns

def f(): return 0

Let us try it:

Python 2.5.1 (r251:54863, Jan 26 2008, 01:34:00)
[GCC 4.1.2 (Gentoo 4.1.2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

I do not want the function to return 0 but to equal 0.

Martin
 
P

Peter Otten

Martin said:
Is there a way to create a function that is equal to 0?
I try to redefine __cmp__ but I am pretty stuck.

Something like:

True

Thanks in advance

Martin

Use a callable object:
.... def __cmp__(self, other): return cmp(other, 0)
.... def __call__(self): return ""
....True

Peter
 
A

attn.steven.kuo

Hi,

Is there a way to create a function that is equal to 0?
I try to redefine __cmp__ but I am pretty stuck.

Something like:


True


You would have to bind f (the name)
to 0 (or False). You can "cheat"
and use a decorator:
.... def f(): return 1
True
 
A

Arnaud Delobelle

Hi,

Is there a way to create a function that is equal to 0?
I try to redefine __cmp__ but I am pretty stuck.

Something like:


True

Why do you want to do that?
... def __init__(self, f): self.f = f
... def __call__(self, *args, **kwargs): return self.f(*args,
**kwargs)
... def __eq__(self, other): return other == 0
...... def f(x): print "Why oh why,", x
...
 
M

Martin Manns

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()

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
 
B

Ben Finney

Martin Manns said:
I do not want the function to return 0 but to equal 0.

Then it seems you don't want a function, but something else. Functions
are functions, not integers.

What problem are you trying to solve, and why do you think this
behaviour will help?
 
G

Gabriel Genellina

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()

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.


If you drop this condition then you could fill the array with zeroes
as before, replace only the interesting ones with actual functions,
and write:

for item in myarray[nonzero(myarray)]:
print item() if callable(item) else item
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.

You can't - anyway, being Python code, would not help with the speed.
 
J

John Machin

If you drop this condition then you could fill the array with zeroes
as before, replace only the interesting ones with actual functions,
and write:

for item in myarray[nonzero(myarray)]:
print item() if callable(item) else item

Let's unbuckle the seat belt :)

If "item" is definitely restricted to being either 0 or a callable
object, then
item() if item else 0
should give the same answer as, and is quite likely to be faster than,
item() if callable(item) else item
as it avoids a global lookup and a function call.

Cheers,
John
 
M

Martin Manns

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.

If you drop this condition then you could fill the array with zeroes
as before, replace only the interesting ones with actual functions,
and write:

for item in myarray[nonzero(myarray)]:
print item() if callable(item) else item

Works for me.

Thank you

Martin
 
G

Gabriel Genellina

If you drop this condition then you could fill the array with zeroes
as before, replace only the interesting ones with actual functions,
and write:
for item in myarray[nonzero(myarray)]:
    print item() if callable(item) else item

Let's unbuckle the seat belt :)

If "item" is definitely restricted to being either 0 or a callable
object, then
    item() if item else 0
should give the same answer as, and is quite likely to be faster than,
    item() if callable(item) else item
as it avoids a global lookup and a function call.

In that case, we could use a bare item() because nonzero would have
filtered out all that zeroes.
 
J

John Machin

If you drop this condition then you could fill the array with zeroes
as before, replace only the interesting ones with actual functions,
and write:
for item in myarray[nonzero(myarray)]:
print item() if callable(item) else item
Let's unbuckle the seat belt :)
If "item" is definitely restricted to being either 0 or a callable
object, then
item() if item else 0
should give the same answer as, and is quite likely to be faster than,
item() if callable(item) else item
as it avoids a global lookup and a function call.

In that case, we could use a bare item() because nonzero would have
filtered out all that zeroes.

True.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top