Python Decorators - __call__ in class

The output of your code is

inside myDecorator.__init__()
inside aFunction()
Finished decorating aFunction()
inside myDecorator.__call__()

First, do you know, what this @ decorator syntax mean?

@decorator
def function(a):
    pass

is just another way of saying:

def function(a):
    pass
function = decorator(function)

So, in Your case

@myDecorator
def aFunction():
   print ("inside aFunction()")

means just

def aFunction():
    print ("inside aFunction()")
aFunction = myDecorator(aFunction)

At first, You basically create a new instance of myDecorator class, invoking it's constructor (__init__) and passing to it a aFunction function object. Then, it executes print and given function. Also, note that this is happening while the function is loaded by interpreter, not when it's executed, so in case you import something from this file, it will execute then, not on use or call.

Then, executing the aFunction(), when aFunction is still refering to the myDecorator instance, makes it to call the __call__ method of myDecorator, which executes. Note, that f() means the same as f.__call__(f) in this case, as __call__ method is used to enable and override default object's calling behaviour (in simplification, any object is callable when it has __call__ method defined).

If you want to execute aFunction when it's called, then you should assign it to instance variable in __init__ and invoke it in __call__ of myDecorator.


That's the entire purpose of a decorator: to replace (or, more usually, wrap) a function with the function returned by the decorator. In your case, aFunction is being replaced with an instance of myDecorator, so when you call aFunction() you are really calling that instance: and in Python, calling a class instance invokes its __call__ method.

Defining a decorated function is exactly equivalent to this:

def aFunction():
    print("inside aFunction()")
aFunction = myDecorator(aFunction)

Usually, of course, the wrapping function would call the original function after doing whatever it does.