Skip to content
Advertisement

Python: How to pass variables into decorators AND decorators pass decorator variables back into function as well?

I am always looping through the files of a directory to perform various kinds of data manipulations. Thus, I always use the following code

JavaScript

Instead of keep writing these lines of code for every function, I was wondering if I can make it better by using decorators. Like the following:

JavaScript

I have found many resources out there which focus on how to pass parameters into the decorator. But in this case, it requires not only pass parameters into the decorator, also require decorator pass parameters to function.

Does anyone know how to achieve?

Advertisement

Answer

The function that takes arguments for the decorator function (wrapper_layer1 in your example) should return the decorator function, not itself. Similarly, the decorator function (wrapper_layer2 in your example) should return the wrapper function, not itself. And finally, the wrapper function (wrapper_layer3 in your example) should return something other than itself to the caller, if at all:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement