The world’s leading publication for data science, AI, and ML professionals.

Why we should use Python Decorator more often

Python Decorator is a powerful tool which can help us manipulate functionality of a function, a test method just by adding @decorator_name

Python Decorators

Photo by Markus Spiske on Unsplash
Photo by Markus Spiske on Unsplash

In Python, everything is object, which means every entity has some metadata (called attributes) and associated functionality (called methods). These attributes and methods are accessed via the dot syntax. Names that we define are simply identifiers bound to these objects. Functions are no exceptions, they are objects too (with attributes). Various different names can be bound to the same function object.

For example:

Here we assign the function to a variable. We will notice that after the variable is assigned, it became a function. Below is the output of the test_decorator:

test_decorators.py::test_decorator PASSED                                [100%]8
3

Functions in Python can also take other function as input, then return other function. This technique is called Higher Order Function.

Here we pass the function (double or triple) as the input for function operate. When run the test_decorator we will see this in console:

test_decorators.py::test_decorator PASSED                                [100%]6
9

Functions and methods are called callable as they can be called.

In fact, any object which implements the special __call__() method is termed callable. So, in the most basic sense, a decorator is a callable that returns a callable.

Basically, a decorator takes in a function, adds some functionality and returns it.

Run the test_decorator will show

test_decorators.py::test_decorator PASSED                                [100%]I got decorated
I am ordinary
None

Often, we will see decorator written in a syntactic sugar way using @decorator_name

Run the test_decorator will show the same

test_decorators.py::test_decorator PASSED                                [100%]I got decorated
I am ordinary
None

Simple decorator to play along

This one is a simple decorator to print Start time and End time for the method which will use the decorator.

In the console, you will see

test_decorators.py::test_decorator_simple PASSED                         [100%]Start time is : 2020-07-21 14:41:44.064251
Time in test : 2020-07-21 14:41:44.064337
End time is : 2020-07-21 14:41:44.064366

Decorators with arguments

Running the test code will show this to console

test_decorators.py::test_decorator_with_arguments PASSED                 [100%]Inside wrapped_f()
Decorator arguments: Donald Le 1990
Testing decorator with arguments
After f(*args)

Chaining Decorators in Python

Multiple decorators can be chained in Python.

This is to say, a function can be decorated multiple times with different (or same) decorators. We simply place the decorators above the desired function.

Running this will show

test_decorators.py::test_decorator PASSED                                [100%]..............................
Start time is 2020-07-22 10:35:17.462552
Test Decorator
End time is 2020-07-22 10:35:17.462579
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Decorators integrate with other framework such as pytest

Then we can use this in the test method

Curious how the value of the decorator can be used, it is actually used in the pytest hook for collect testrail id.

This is how we get the decorator value from pytest:

testrail_ids = item.get_closest_marker('testrail').kwargs.get('ids')

That’s it.

Thanks for reading my post.


References

https://www.geeksforgeeks.org/decorators-in-python/

Decorators – Python 3 Patterns, Recipes and Idioms

Python Decorators

Note: If you like this story and want to read similar stories like this, and you do not have Medium subscription YET, please subscribe Medium from this link https://ledinhcuong99.medium.com/membership. This can support me for writing content like this. Thank you!


Related Articles