473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can I make a function equal to 0?

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
8 2066
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave | last post by:
Hello all, Quoting from page 24 of "The Boost Graph Library; User Guide and Reference Manual": "It turns out that by the contravariance subtyping rule, the parameter type in the derived...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
1
by: JohnPantango | last post by:
Can anyone give me a simple work around to fix this compiler error on VC7.1 .....cpp(23) : error C2912: explicit specialization; 'bool Equal<_F32>(const _F32::type,const _F32::type)' is not a...
29
by: Vol | last post by:
I think 'atan' can get the angle but it is not the four quadrant angle. Is there any function that i can get the angle from -pi to pi? or I have to use some if ... else? I know in Matlab, we use...
5
by: Ian Bicking | last post by:
I got a puzzler for y'all. I want to allow the editing of functions in-place. I won't go into the reason (it's for HTConsole -- http://blog.ianbicking.org/introducing-htconsole.html), except that...
5
by: pat270881 | last post by:
hello, i should implement this class: namespace test_1 { class statistician { public: // CONSTRUCTOR
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
10
by: Matthew Wilson | last post by:
Lately, I've been writing functions like this: def f(a, b): assert a in assert b in The point is that I'm checking the type and the values of the parameters.
1
by: rush.william | last post by:
Hi All! Is there any way to compare 2 boost::function<objects? boost::function has no operator==() for comparing boost::function<>. Is there any hack way? I need to do the following: struct A...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.