I am trying to decorate an instance method with a decorator class, but it fails to pass on the instance as self
to the decorated callable. Wrapping into @wraps
does not work either, because the wrapped method is not available yet while building the decorator class:
#!/usr/bin/env python3
"""Trying to decorate a method."""
class Wrap:
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print("self: {self}\n*args: {args}\nkwargs: {kwargs}".format(
self=self, args=args, kwargs=kwargs))
self.func(self=args[0], a=3.141)
class A:
@Wrap
def aaa(self, a=None):
print("My instance: {}\na = {}".format(self, a))
a = A()
a.aaa(1000)
# My instance: 1000
# a = 3.141
## The object "a" is not passed on, rendering aaa effectively a static method.
a.aaa(a, 1000)
# My instance: <__main__.A object at 0x7fe371579be0>
# a = 3.141
## The expected result for the first attempt.
How can I access a
inside __call__
, without passing it explicitly?