2 ms·
For comparisons sake: class Thing: def __init__(self, arg): # expensive precompute self.result = ... def __call__(self, arg):
by fastball 6y ago
For comparisons sake:
class Thing:
def __init__(self, arg):
# expensive precompute
self.result = ...
def __call__(self, arg):
# cheap stuff that uses self.result for something
...
And then usage is:
f = Foo("some val")
bar = f("another val")
So yeah, both definitely do the same job. And your pattern is probably faster. But I generally like using Python's dunder methods as I think they allow for more consistent/recognizable code patterns.