Thursday, 18 February 2021

Mastering Python Decorators: Understanding Function Composition and Decorator Usage

 




Things to Know before proceeding for Decorators In Python:



Meta Description: 

Learn about decorators in Python and their usage in function manipulation. Explore examples and gain insights into the concept of decorators.

Introduction

When working with functions in Python, it's essential to understand the flow and possibilities they offer. In this blog, we will explore the concept of decorators and delve into important concepts related to function manipulation. By the end, you'll have a solid grasp of decorators and their usage.

Calling Functions Inside Functions: 

Before diving into decorators, let's start by examining a code snippet that demonstrates calling a function inside another function. This simple example showcases the ability to nest function calls in Python.

Function Composition Example:



 

Understanding Function Composition:

By calling one function inside another, we observe the ability to create function compositions. This allows us to build more complex functionality by combining simpler functions.

The above code will return <class 'int'> , Here we see int and str are built in functions in python. from above Python code we can see that we can call a function inside a function.


Different Approaches to Function Composition:

 





This code will return Hello Harry How are you. Here I Created two user defined functions. function one_fun takes a string as a parameter and return Hello followed by Harry as its input. The trick is here . The second function sec_fun takes first function  one_fun as a input and will return How are you after first function called in second function. Some programmer will assign the result of first function to a variable with in second function and then return result. This is depend on coder to coder.




 

This code will return Hello Harry How are you. but here is some change in approach. created resukt (Please dont go on name i dont know what is this😆) variable and called second function with input of first one but i didnt call first one( When you use bracket after function it means you are calling that function). and i stored the result of this second function in variable. then while printing i called variable(stored value of second function now variable behave as a function) and provide the name as an input. if i give name harry input while assigning function value to resukt it will behave like sec_fun(one_fun("Harry")) code used in above example.

In the above example, we showcase two different approaches to function composition. Each approach offers flexibility, and the choice depends on the coder's preference and requirements.


Introduction to Decorators:


===========================================================

Now that we have a foundation in function composition, let's explore the concept of decorators. 

"A Decorator takes a function as a parameter, perform some additional logic and return function."

Now check last example we have seen second function sec_fun is the function which takes first function one_fun as a parameter, perform a logic inside inner function and return inner function.

Now i assume that you know what is Decorators in python. Now lets go one step ahead.
We use @ symbol to define decorators, where to use see below code of same example.




Here I used @sec_fun line instead of resukt = sec_fun(one_fun) . So you could say @sec_fun gives same result as resukt = sec_fun(one_fun).

Conclusion:

In this blog, we covered important concepts related to decorators in Python. We discussed function composition, the use of decorators to enhance functionality, and the simplified syntax provided by the @ symbol. By understanding decorators, you can leverage their power to manipulate and extend the behavior of functions in your Python code.


Resources - code with harry YouTube channel



Post Top Ad

Your Ad Spot

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template