I need to implement a decorator with classes or functions that receive parameters, like the following example
@MyDec(flag='foo de fa fa')
def bar(a,b,c):
print('En bar(...) : ',a,b,c)
I need to implement a decorator with classes or functions that receive parameters, like the following example
@MyDec(flag='foo de fa fa')
def bar(a,b,c):
print('En bar(...) : ',a,b,c)
Initially we create a class
MyDec
that is going to be the decorator, and in the constructor it receives all the arguments that are necessary in this case we add the argumentflag
. In the method__call__(self)
that is the one that is executed when we add the decorator@MyDec(flag='foo de fa fa')
in the function. Within the arguments of__call__
this ,original_func
which is the function to which the decorator has been placed. And insidewrappee()
we receive the arguments of the function and later we execute the functionoriginal_func(*args,**kwargs)
and we pass the arguments and the KeyWordsArguments