How can I make a function equal to 0?

Arnaud Delobelle arnodel at googlemail.com
Fri Mar 21 16:12:56 EDT 2008


On Mar 21, 7:48 pm, Martin Manns <mma... at gmx.net> wrote:
> 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

Why do you want to do that?

>>> class WhyOhWhy(object):
...     def __init__(self, f): self.f = f
...     def __call__(self, *args, **kwargs): return self.f(*args,
**kwargs)
...     def __eq__(self, other): return  other == 0
...
>>> @WhyOhWhy
... def f(x): print "Why oh why,", x
...
>>> f("do this?")
Why oh why, do this?
>>> f == 0
True
>>>

--
Arnaud




More information about the Python-list mailing list