1

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?

lc2817
  • 3,722
  • 16
  • 40
  • 1
    The error mentions a `match` object rather than a `matcher` object. What type of objects does `self._matchers` contain? – TigerhawkT3 Jan 22 '16 at 06:55
  • The example I put is just informative, fixing it. `self._matchers` contains `match` objects have described above. – lc2817 Jan 22 '16 at 06:56
  • Thanks for the help :) – lc2817 Jan 22 '16 at 07:02
  • related: [What is a “callable” in Python?](http://stackoverflow.com/q/111234/4279) – jfs Jan 22 '16 at 07:07
  • 1
    You're using the name "match" in too many places. It's the name of a class, it's the index variable in the for loop, and it's a function call in `if match(value)`. Can you figure out how the interpreter will handle this mess? What will pdb do? I don't want to think about it. Rename something and try your program again. – Paul Cornelius Jan 22 '16 at 07:18

0 Answers0