Python Function Decorators

In this Python article we want to learn about Python Function Decorators, function decorators are powerful tool for modifying and enhancing the behavior of functions. with decorators you can add functionality to a function without changing its source code. Decorators can be used for different purposes, from logging to memorization to performance profiling. you can create decorators in different ways.



One way of creating decorators is to use randomization. for example you want to create a decorator that randomly modifies the output of a function. 

In this example random_decorator randomly converts the output of the greet function to uppercase or lowercase.



This will be the result

Python Function Decorators
Python Function Decorators



Another way for creating Python decorators is to use external APIs or services. for example, you want to create a decorator that adds random image or quote to the output of functions by calling an external API. 

In this example random_image_decorator calls Unsplash API to retrieve random landscape image and adds it to the output of the greet function. 




Leave a Comment