dimod.decorators.forwarding_method#
- forwarding_method(func)[source]#
Improve the performance of a forwarding method by avoiding an attribute lookup.
The decorated method should return the function that it is forwarding to. Subsequent calls will be made directly to that function.
Example
>>> import typing >>> import timeit >>> from dimod.decorators import forwarding_method ... >>> class Inner: ... def func(self, a: int, b: int = 0) -> int: ... "Inner.func docsting." ... return a + b ... >>> class Outer: ... def __init__(self): ... self.inner = Inner() ... ... def func(self, a: int, b: int = 0) -> int: ... "Outer.func docsting." ... return self.inner.func(a, b=b) ... ... @forwarding_method ... def fwd_func(self, a: int, b: int = 0) -> int: ... "Outer.fwd_func docsting." ... return self.inner.func ... >>> obj = Outer() >>> obj.func(2, 3) 5 >>> obj.fwd_func(1, 3) 4 >>> timeit.timeit(lambda: obj.func(10, 5)) 0.275462614998105 >>> timeit.timeit(lambda: obj.fwd_func(10, 5)) 0.16692455199881806 >>> Outer.fwd_func.__doc__ 'Outer.fwd_func docsting.' >>> obj.fwd_func.__doc__ 'Inner.func docsting.'