Data & Business Analysis

Python Decoretors - All You Need To Know

Sep 14, 2024

Master Python Decorators: The Secret Sauce to Cleaner, More Powerful Code!

Topics covered:


  • What are decorators and 3 different ways to use them?
  • Critical concepts before implementing Decorators:
  • Function copy / function as objects
  • Passing function as an argument inside another function
  • Returning Function from another function
  • A Simple example of Decorators with Code (wrapper, *args, **kwargs, func__name__ , @)
  • How decorators are different than a regular function?


What are decorators ?


  • Decorators are a kind of function which can be wrapped inside another function to extend the behaviour of that wrapped function into the calling function.


  • Decorators are set of codes which are reusable.


  • you call the decorators using @ symbol at the start of the decorator.


3 Different places you can call the decorators



3 Critical concepts Before Implementing Decorators


1. Function copy / function as objects

2. Returning Function from another function

3. Passing function as an argument inside another function


In Python, functions are first class objects which means that functions in Python can be used or passed as arguments.


Properties of first class functions:


  • A function is an instance of the Object type.
  • You can store the function in a variable.
  • You can pass the function as a parameter to another function.
  • You can return the function from a function.
  • You can store them in data structures such as hash tables, lists, …



1. Function copy / function as objects


  • In the below example we have assigned the function TSFStudents to a variable called GotJob


  • This will not call the function TSFStudents and assign the result to the variable GotJob


  • Instead it takes the function object referenced by the function TSFStudents and creates a second name pointing to it the variable GotJob


  • So even after i delete the original function TSFStudents, the GotJob will still return the same function’s o/p becasue it has copied the function as an object



Closures


In python when you create a function inside another function, you will be able to access the variable from the parent functions inside the child function.


2. Returning Function from another function


  • In the below example we have created a function named child_func inside main function named parent_fun


  • in the main function we have passed an argument “msg”


  • this “msg” variable is accecible inside the inner function named child_fun as well - concept closures.


3. Passing function as an argument inside another function


In this code, the TSF_Print function accepts another function (x) as an argument, applies it to a string, and prints the result.



A Simple Example of A Decorator


The below code measures how much time it takes for two functions, calc_square and calc_cube, to compute the squares and cubes of a list of numbers, respectively.


It does this by using the time module to track the time before and after the computation, then printing the elapsed time in milliseconds.


First lets understand before implementing Decoretors, how the code will look:


Before Decorators


  • the yellow highlighted lines are repeated code, which can be converted into a decorator and we can reuse.


  • This is a simple code, imagine you have a 10000 line of codes, in that case decorators becomes very handy w.r.t code update and code reuse.



After Decorators


  • We have created a decorator function names time_me


  • The wrapper function is defined inside time_me. This is a common pattern in decorators.


  • The wrapper function accepts any number of arguments (*args) and keyword arguments (**kwargs). This ensures that time_me can be used with functions that have any kind of parameters.


  • The print statement uses an f-string to display the name of the function (func.__name__) and the calculated time in milliseconds.




How decorators are different than a function?