Decorator functions in Python

Aishwarya Shilpi
Nerd For Tech
Published in
3 min readJun 13, 2021

--

Decorator functions

This short note is about using decorators in python on a very basic level and where it's used. We will see the most usual usage of decorator functions and then understand how we can write our own.

Web development in general

Web development includes working full-stack, that is both on the front end and backend. For the development, apart from the choice of language, you can also choose between the frameworks that you can use for the front end like Angular or React and the back end like Django, Flask, or Node.

Frameworks are basically tools that come with a lot of prebuilt for a lot of common functionality. That way we don’t always have to reinvent the wheel, so we can just use the wheel for building cycles or cars or airplanes.

In the front end, HTML provides the structure, CSS with the style, and JS with the interactivity also constitutes the client. The backend has both the business logic on the server as well as the data, you guessed it on the database.

For the easiest example, consider the restaurant, where when you go, you sit in the dining area (the client), your food is prepared in the kitchen(the server) and the storage of the food (the database) is probably in the larder if not the fridge.

Library vs Framework

You would have probably used a library a couple of times, it's where you are in full control when you call a method, and the control is then returned. The main difference between the library and a framework is the code never calls into a framework, instead, the framework calls the code. Like in this case.

Basic flask framework application

The name of the file should never be in conflict with the framework or library.

and then calling this from the terminal the CLI.

export FLASK_APP=“calling_flask_framework.py”

flask run

For understanding the operating system, take an analogy of it being pistachio. So the kernel is the nut, it's the actual program that interfaces with the hardware, it's the core of OS. Then the cover will be the shell, or the UI, or the CLI.

For understanding this piece of code, _name_ is a special attribute inside python, which returns the current working script name. And the @ is syntactic sugar for a decorator function that would be defined inside the framework. The decorator function gives additional functionality to an existing function.

Functions are first class citizens in python

For understanding decorators you also need to know, when we treat functions as first-class objects, without (), it can be passed around as arguments and can also return a function from another function.

Removing parenthesis from a function means that we are no longer going to activate it there.

Nested Functions

In python, we can also define a nested function that looks like this.

Python decorators

So finally coming to define the python decorator:

The decorator wraps another function and gives it modified or additional functionality. Line 3 to 6 defines the most basic decorator function which just passes the function as it is and does nothing.

From lines 15 to 19, the function will be passed with a delay of 2 seconds before calling the function. And hence when we call the decorator using @ before any method, it's passed inside it and the repetitive code can be implemented before and after it.

Here is an example on testing speeds of various functions using decorators in python.

@app.route is a decorator for the flask framework, which calls the function whenever the route is hit. It was a key piece of information for me. And hope it helps you too. Let me know your thoughts. Happy Coding!

--

--