I have an instance A
a which is called repeatedly like A(i)
. Since this call is the bottleneck I want to code A.__call__()
as perfomant as possible. Gains could be made if I can leverage the information which is available during instance creation, and select the call method based on this information, as in this mock example:
class xxx():
def __init__(self, **kwargs):
if kwargs["foo"] == "bar":
self.__call__ = self.do_B
else:
self.__call__ = self.do_C
def do_A(self):
return 2
def do_B(self):
return 3
A = xxx(foo="bar")
A()
I am aware that there are plenty of posts on how to achieve this after the instance has been created, and that I could simply call A._do_B(i)
in my code, but this would clutter the code with plenty of ifs and the above seems to be more natural (to me anyway :-)