overloading operators for a function object
Chris Rebert
clp2 at rebertia.com
Sat Oct 1 01:00:33 EDT 2011
More information about the Python-list mailing list
Sat Oct 1 01:00:33 EDT 2011
- Previous message (by thread): overloading operators for a function object
- Next message (by thread): overloading operators for a function object
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Fri, Sep 30, 2011 at 9:50 PM, Fletcher Johnson <flt.johnson at gmail.com> wrote: > Is it possible to overload operators for a function? > > For instance I would like to do something roughly like... > > def func_maker(): > def func(): pass > > def __eq__(other): > if other == "check": return True > return False > > func.__eq__ = __eq__ > return func > > newfunc = func_maker() > newfunc == "check" #true > newfunc == "no" #false You can write a callable [wrapper] object to get the same effect: class SpecialFunction(object): def __call__(self, actual, args, go, here): return whatever def __eq__(self, other): return other == "check" newfunc = SpecialFunction() newfunc == "check" # => True newfunc(1, 2, 3, 4) #=> whatever Cheers, Chris -- http://rebertia.com
- Previous message (by thread): overloading operators for a function object
- Next message (by thread): overloading operators for a function object
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list