I am trying to optimize the performance of an object and switched from this pattern:
class match(object):
def __init__(self, fn):
self.matchfn = fn
def __call__(self, val):
return self.matchfn(val)
to
class match(object):
def __init__(self, fn):
self.__call__ = fn
It is particularly important as __call__
is called hundreds of thousands of time on this object.
From testing in a shell, it seems to work and be much faster but once integrated with all the rest of my code it does not work.
For an obscure reason my instance has a __call__
method but isn't considered callable.
Here is a pdb session I used to try to debug the issue:
844 def __call__(self, value):
845 for match in self._matchers:
846 import pdb; pdb.set_trace()
847 -> if match(value):
848 return True
849 return False
(Pdb++) print match.__call__
<function <lambda> at 0x1258b0a28>
(Pdb++) print match.__call__(value)
False
(Pdb++) print match(value)
*** TypeError: 'match' object is not callable
What could I be missing?