1

I'm reading Learning Python by Mark Lutz. I'm currently reviewing chapter 32, Part 1 which discusses Decorators and Metaclasses. Lutz states that the class below cannot be used to decorate class-level method functions. I can see this is true but I'm have some trouble conceptualizing why. As far as I can tell, inside the __call__ method self is equal to t, an instance of tracer and args is equal (a,b,c). Shouldn't self.func(*args) resolve to tracer.func(t,a,b,c)? Instead, inside the method I'm calling self becomes equal to 1. why is that?

class tracer:
    def __init__(self,f):
        self.counter = 0
        self.func = f
    def __call__(self,*args):
        self.counter += 1
        return self.func(*args)

class Test:
    @tracer
    def spam(self,*args):
        print(a+b+c)

t = Test()
t.spam(1,2,3)
t.spam(1,2,3)
t.spam(1,2,3)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
chandler
  • 11
  • 1

0 Answers0