Working with decorators in Python - The Basics

by Davide Mastromatteo
6 minute read

teaser

Hi Guys, in today’s article, we will discuss Python decorators.

Decorators are not a python-only feature, they exist in many other languages and are important if you aim to write clean, professional, and reusable code.

A decorator is a function that extends other functions that are passed as a parameter, adding new features without the need of changing their code.

This can seem a little magical to a beginner’s eye, but it’s really easy actually because all you have to do is to create a function (the so-called “decorator function") that takes another function as an argument (the “decorated function") and returns a completely new function that simply modify the behavior of your decorated function. That’s made possible by the fact that in Python, functions are first-class citizens and can be passed as arguments to other functions. I wrote a post about that if you’re interested in this topic.

It may seem something difficult that needs a 5000 words article to be explained… but it’s not! Let’s see a practical example.

Let’s pretend we are writing a program that prints out 10 integers between 0 and 100. This is a possible implementation:

1import random
2
3def get_rnd():
4    return random.randrange(100)
5
6for i in range(1,11):
7    my_random_number = get_rnd()
8    print (f"Number {i}: {my_random_number}")

This script is quite trivial and if you run it you will see 10 random numbers. This is the output I got:

Number 1: 54
Number 2: 28
Number 3: 82
Number 4: 12
Number 5: 94
Number 6: 14
Number 7: 57
Number 8: 73
Number 9: 51
Number 10: 72

Nice formatted output uh? That’s because I’ve used f-strings! Go and check out more with my article about f-strings.

Now let’s pretend that you are asked to extract not just 10 numbers, but 10 odds numbers. How can you modify your program? One possibility is to test the number inside the get_rnd() function and just keep extracting until an odd number gets extracted. This is a solution, but another approach could be leaving the get_rnd() function unmodified and create a decorator function to modify the behavior of the get_rnd() function.

Let’s try it! At first we will write a decorator function named only_odd() that will take a function as a parameter (later we will pass the get_rnd() function as a parameter). Inside this function, we will define another function (the inner() function) that just keeps calling the get_rnd() functions until an odd number is returned.

Here’s the code of our decorator:

1def only_odd(func):
2    def inner(): # here we define a new function that will be returned
3        while True: 
4            my_number=func() # this function will just keep calling func
5            if my_number%2!=0:  # until a odd number is returned
6                return my_number
7            else:
8                print(f"{my_number} is even, retry...")
9    return inner

Yes, this is a decorator. Now, our program could be modified like this:

 1import random
 2
 3def only_odd(func):
 4    def inner():
 5        while True:
 6            my_number=func()
 7            if my_number%2!=0:
 8                return my_number
 9            else:
10                print(f"{my_number} is even, retry...")
11    return inner
12
13def get_rnd():
14    return random.randrange(100)
15
16for i in range(1,11):
17    # here we create a decorated function...
18    my_new_decorated_function_to_get_random_odd = only_odd(get_rnd)
19    # ... that we will call to get a random odd
20    my_random_number = my_new_decorated_function_to_get_random_odd()
21    print (f"Number {i}: {my_random_number}")

And now, if I run this program, what I get is:

Number 1: 85
Number 2: 97
Number 3: 21
10 is even, retry...
Number 4: 21
Number 5: 57
52 is even, retry...
50 is even, retry...
42 is even, retry...
70 is even, retry...
Number 6: 17
Number 7: 7
Number 8: 79
44 is even, retry...
Number 9: 95
Number 10: 83

You can see that our decorator has worked great and each time the get_rnd() function returned an even number, it kept calling it again to wait for an odd one.

Cool! But look at our new call:

1    # here we create a decorated function...
2    my_new_decorated_function_to_get_random_odd = only_odd(get_rnd)
3    # ... that we will call to get a random odd
4    my_random_number = my_new_decorated_function_to_get_random_odd()
5    print (f"Number {i}: {my_random_number}")

We decided to use a decorator because we wanted to modify the behavior of our get_rnd() function without affecting the caller, that’s not what we have now. Luckily, Python here helps us with some “syntax sugar”.

In fact, we can decorate our get_rnd() just by using the @ symbol, followed by the name of our decorator function, before the declaration of the function we want to decorate. Doing this, we don’t need anything else to call our decorated function, and our program could be like this:

 1import random
 2
 3def only_odd(func):
 4    def inner():
 5        while True:
 6            my_number=func()
 7            if my_number%2!=0:
 8                return my_number
 9            else:
10                print(f"{my_number} is even, retry...")
11    return inner
12
13@only_odd # this decorate our get_rnd() function
14def get_rnd():
15    return random.randrange(100)
16
17for i in range(1,11):
18    my_random_number = get_rnd() # look, no change to the caller!
19    print (f"Number {i}: {my_random_number}")

Isn’t that cool? And the coolest part is that our @only_odd decorator can be reused on all the functions that return a single integer. For example, let’s say that we have also a function that takes a numeric input from user keyboard and returns an integer, like this:

1def square_input():
2 while True:
3    input_text=input("Enter a number:")
4    try:
5        input_number = int(input_text)
6        return(input_number**2)
7    except:
8        print("not a number")

If we wanted to keep asking for numbers until the result is an odd number, we just need to decorate our function.

Here’s our example modified:

 1import random
 2
 3def only_odd(func):
 4    def inner():
 5        while True:
 6            my_number=func()
 7            if my_number%2!=0:
 8                return my_number
 9            else:
10                print(f"{my_number} is even, retry...")
11    return inner
12
13@only_odd # this decorate our get_rnd() function
14def get_rnd():
15    return random.randrange(100)
16
17@only_odd # this decorate our square_input() function
18def square_input():
19 while True:
20    input_text=input("Enter a number:")
21    try:
22        input_number = int(input_text)
23        return(input_number**2)
24    except:
25        print("not a number")
26
27print("Your odd numbers:")
28for i in range(1,11):
29    my_random_number = get_rnd() # look, no change to the caller!
30    print (f"Number {i}: {my_random_number}")
31
32print("---")
33
34print("Now, let's play with squares:")
35my_squared_input = square_input()
36print (f"The square of your number is {my_squared_input}")

And running the previous example I got this output:

Your odd numbers:
28 is even, retry...
54 is even, retry...
Number 1: 81
Number 2: 81
62 is even, retry...
2 is even, retry...
44 is even, retry...
26 is even, retry...
Number 3: 59
Number 4: 87
60 is even, retry...
Number 5: 29
78 is even, retry...
Number 6: 83
Number 7: 25
Number 8: 79
Number 9: 71
50 is even, retry...
82 is even, retry...
70 is even, retry...
0 is even, retry...
36 is even, retry...
Number 10: 3
---
Now, let's play with squares:
Enter a number:4
16 is even, retry...
Enter a number:77
The square of your number is 5929

Ok guys, let’s call this an article! :) In this first part, I introduced you to the wonderful world of decorators, in the next one I will show you some other powerful use of decorators, like how to use decorators with arguments or how to decorate a function that can take arguments itself.

So… stay tuned for the next one!

Happy Coding and … happy new year! D.

Note: All the code above has been tested using Pyto iOS app on a simple iPhone. It has been great being able to write an article without having access to a real full-size computer running a Python interpreter, so let me reccomend this great piece of freemium software.


Did you find this article helpful?


Buy me a coffee! Buy me a coffee!