How can I make a function equal to 0?

Peter Otten __peter__ at web.de
Fri Mar 21 21:04:28 CET 2008


Martin Manns wrote:

> 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

Use a callable object:

>>> class F(object):
...     def __cmp__(self, other): return cmp(other, 0)
...     def __call__(self): return ""
...
>>> f = F()
>>> f()
''
>>> f == 0
True

Peter



More information about the Python-list mailing list