1

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?

quazgar
  • 4,304
  • 2
  • 29
  • 41
  • Part of the problem is that `aaa` is no longer a descriptor, since `Wrap` doesn't define `__get__`. As a result, `a.aaa` is just a function, not a method. – chepner May 02 '19 at 16:36
  • Yes, that's probably the problem and [30104047](https://stackoverflow.com/questions/30104047) is the same question indeed. – quazgar May 02 '19 at 16:51
  • I'm glad Aran-Fey found that, because I was blanking on how to define `Wrap.__get__` properly :) – chepner May 02 '19 at 16:57

0 Answers0