Decorators are powerful things in most programming languages. They help us making code more readable and adding functionality to a method or class. Basically, decorators are added above the method or class declaration in order to create some behaviour. Basically, we differentiate between two kind of decorators: method decorators and class decorators. In this tutorial, we will have a look at Method decorators.
Method decorators are used to perform some kind of behaviour on a method. For instance, you could add a stopwatch to check for performance, configure logging or make some checks on the method itself. All of that is done by “wrapping” the method into a decorator method. This basically means that the method “decorated” is executed in the decorator method. This, for instance, would allow us to surround a method with a try-catch block and thus add all exceptions occurred in a method into a global error handling tool.
The definition of that is very easy:
Basically, the only thing that you need is the “@” and the decorator name. There are several decorators available, but now we will create our own decorator. We start by creating a performance counter. The goal of that is to measure how long it takes a method to execute. We therefore create the decorator from scratch.
Basically, I stated that the decorator takes the function and executes it inside the decorator function. We start by defining our performance counter as function, that takes one argument – the function to wrap in. Within this function, we add another function (yes, we can do this in Python – creating inline functions!) – typically we call it either “wrapper” or “inner”. I call it “inner”. The inner function should provide the capability to pass on arguments; typically, a function call can have 0 to n arguments. In order to do this, we provide “*args” and “**kwargs”. Both mean that there is a variable number of arguments available. The only difference between args and kwargs is that kwargs are named arguments (e.g. “person = “Pete”).
In this inner function, we now create the start-variable that is the time once the performance counting should start. After the start-variable, we call the function (any function which we decorate) by passing on all the *args and **kwargs. After that, we measure the time again and do the math. Simple, isn’t it? However, we haven’t decorated anything yet. This is now done by creating a function that sleeps and prints text afterwards. The code for this is shown below.
import time def perfcounter(func): def inner(*args, **kwargs): start = time.perf_counter() func(*args, **kwargs) #This is the invokation of the function! print(time.perf_counter() - start) return inner @perfcounter def printText(text): time.sleep(0.3) print(text) printText("Hello Decorator")
Hello Decorator 0.3019062000021222
As you can see, we are now capable of adding this perfcounter decorator to any kind of function we like. Normally, it makes sense to add this to functions which take rather long – e.g. in Spark jobs or web requests. In the next sample, I create a type checker decorator. Basically, this type checker should validate that all parameters passed to any kind of function are of a specific type. E.g. we want to ensure that all parameters passed to a multiplication function are only of type integer, parameters passed to a print function are only of type string. Basically, you could also do this check inline, but it is much easier if you write the function once and simply apply it to the function as a decorator. Also, it greatly decreases the number of code lines and thus increases the readability of your code. The decorator for that should look like the following:
For integer values and
for string values.
The only difference now is that the decorator itself takes parameters as well, so we need to wrap the function into another function – compared to the previous sample, another level is added. What are the steps necessary?
- Create the method to get the parameter: def typechecker(type)
- Create the outer function that takes the function and holds the inner function
- Create the function block that holds the inner function and a type checker:
- We add a function called “isInt(arg)” that checks if the argument passed is of a specific type. We can use “isinstance” to check if an argument is of a specific type – e.g. int or str. If it isn’t of the expected type, we raise an error
- We add the inner function with args and kwargs. In this function, we iterate over all args and kwargs passed and check it against the above function (isInt). If all checks succeed, we invoke the wrapped function.
Sounds a bit complex? Don't worry, it isn't that complex at all. Let's have a look at the code:
def typechecker(type): def check(func): def isInt(arg): if not isinstance(arg, type): raise TypeError("Only full numbers permitted. Please check") def inner(*args, **kwargs): for arg in args: isInt(arg) for kwarg in kwargs: isInt(kwarg) return func(*args, **kwargs) return inner return check
Now, since we are done with the decorator itself, let’s decorate some functions. We create two functions. The first one multiplies all values passed to the function. The values can be of variable length. The second function prints all strings passed to the function. We decorate the two functions with the typechecker-decorator defined above.
@typechecker(int) def mulall(*args): res = 0 for arg in args: if res == 0: res = arg else: res *= arg return res @typechecker(str) def concat(*args): res = "" for arg in args: res += arg return res
I guess you can now see the benefit of decorators. We can influence the behaviour of a function and create code-snippets that are re-usable. But now, let’s call the functions to see if our decorator works as expected. Note: the third invokation should produce an error 🙂
print(mulall(1,2,3)) print(concat("a", "b", "c")) print(mulall(1,2,"a"))
… and the error message:
TypeErrorTraceback (most recent call last) <ipython-input-6-cd2213a0d884> in <module> 35 print(mulall(1,2,3)) 36 print(concat("a", "b", "c")) ---> 37 print(mulall(1,2,"a")) <ipython-input-6-cd2213a0d884> in inner(*args, **kwargs) 7 def inner(*args, **kwargs): 8 for arg in args: ----> 9 isInt(arg) 10 11 for kwarg in kwargs: <ipython-input-6-cd2213a0d884> in isInt(arg) 3 def isInt(arg): 4 if not isinstance(arg, type): ----> 5 raise TypeError("Only full numbers permitted. Please check") 6 7 def inner(*args, **kwargs): TypeError: Only full numbers permitted. Please check
I hope you like decorators. In my opinion, they are very helpful and provide great value. In the next tutorial, I will show how class decorators work.