By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

How can I make a function equal to 0?

P: n/a
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:
>>def f(): return ""
....
>># Some magic
f == 0
True

Thanks in advance

Martin
Mar 21 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Martin Manns <mm****@gmx.netwrites:
Is there a way to create a function that is equal to 0?
def f(): return 0
Mar 21 '08 #2

P: n/a
On 21 Mar 2008 12:52:12 -0700
Paul Rubin <http://ph****@NOSPAM.invalidwrote:
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.
>>def f(): return 0
....
>>f==0
False
>>f()==0
True
>>>

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

Martin
Mar 21 '08 #3

P: n/a
On Mar 21, 12:48 pm, Martin Manns <mma...@gmx.netwrote:
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:
>def f(): return ""
...
># Some magic
f == 0

True

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

--
Hope this helps,
Steven
Mar 21 '08 #4

P: n/a
Martin Manns <mm****@gmx.netwrites:
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?

--
\ "Pinky, are you pondering what I'm pondering?" "Well, I think |
`\ so, Brain, but 'apply North Pole' to what?" -- _Pinky and The |
_o__) Brain_ |
Ben Finney
Mar 21 '08 #5

P: n/a
On Mar 22, 8:51 am, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
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
Mar 21 '08 #6

P: n/a
On Fri, 21 Mar 2008 14:51:49 -0700 (PDT)
Gabriel Genellina <ga*******@yahoo.com.arwrote:
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
Mar 21 '08 #7

P: n/a
On 21 mar, 19:25, John Machin <sjmac...@lexicon.netwrote:
On Mar 22, 8:51 am, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
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.

--
Gabriel Genellina
Mar 22 '08 #8

P: n/a
On Mar 22, 3:32 pm, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
On 21 mar, 19:25, John Machin <sjmac...@lexicon.netwrote:
On Mar 22, 8:51 am, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
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.

Mar 22 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.