Decorators Fundamentals by theparthee
Data Analytics

Unlocking the magics of Python Decorators – A Beginners Guide

decorators by the.parthee

Are you a Python beginner who’s ever felt daunted by the concept of decorators? Fear not, for in this article, we’ll unravel the mystery behind Python decorators with a simple and practical example.

Today, we’ll explore how to implement a decorator that adds tax to an existing function, specifically a function that calculates the sum of prices.

By the end of this article, you’ll not only have a clearer understanding of decorators but also a practical tool to enhance your Python coding skills.

Let’s dive in and discover how decorators can simplify your code and make it more efficient! Here is the decorator implementation code.

Decorator Implementation

Certainly, before we dive into the provided code example, it’s important to familiarize ourselves with some fundamental concepts related to Python functions.

  1. Function As Object
  2. Function As Argument
  3. Nested Functions | Returning a Function

These foundational concepts will help us better understand decorators and their utility.

Let’s explore each of these concepts briefly, along with illustrative examples

1. Function As Object

In Python, functions can be used as objects, much like other data types such as int, str, or float. Consider the following example.

Function as Object

When I call the add() function with arguments, I can get the output value and can store it in variable ‘x’.

However, if I assign the function without any argument and parenthesis, it will be stored as an object.

Check out the variable ‘y’ and its data type, you can notice the output as <class ‘function’>

So, now I can use the ‘y’ as a function and pass the arguments to get the desired result.

2. Function As Argument

In Python, we can pass the function as an argument of another function. Consider the example below.

Function As Argument

Here I’ve multiplication and division functions and the function “Operation” has the two arguments ‘x’ and ‘func’.

We can pass any function as argument of “Operation” function, so we can do some preprocessing work before executing the required function.

In this example, we are calculating the y value from some formula before doing multiplication or division function.

Let’s try the output of the above function. Here we don’t know the value of ‘y’ and so we just passing ‘x’ and ‘func’ to operations.

Output from the Function As an Argument

3. Nested Function | Return a Function

Suppose two users need to perform two distinct operations. The first user provides the price input, while the second user assigns the tax rate.

This scenario leads us to explore the concept of nested functions, where one function resides inside another. The combination of ‘Function as an Object’ with ‘Nested Functions’ enables us to cleanly separate the tax calculation operation from acquiring the price value from the user.

Nested Function Example

Let the first user enter the price value, and then have the second user enter the tax value.

Nested Function Example

In this scenario, the calculate_tax function acquires the price value of 100 from the first user, storing it as an object in ‘x.’

Subsequently, the second user enters a tax value of 18, which is applied to the object ‘x’ through a nested function call.

With these fundamental concepts in mind, we are now prepared to explore decorator functions.

Decorator

Decorators are employed to alter the behavior of an existing function without necessitating modifications to its existing code.

In our approach, we leverage a combination of Nested Functions and Functions as Arguments to implement the decorator.

A “special_tax_18” decorator function designed to add a special tax of 18% to the prices passed to a function it decorates, but only if the individual price is greater than or equal to 200.

  1. We are passing the “calculate_bill_value” function as an argument to the decorator function “special_tax_18”.
  2. The function “special_tax_18” returns the preprocessing function “apply_tax” with the arguments of “calculate_bill_value” function.
  3. The preprocessing function “apply_tax” applying the business logic of adding 18% tax to the given prices if the price value is greater than or equal to 200 then returning the tax added values to the regular function “calculate_bill_value”
  4. Now the regular function “calculate_bill_value” do the sum process.
Decorator Implementation

Let’s check out the output here!

Decorator Output

I trust you’ve gained a solid understanding of decorator concepts. Now, let’s dive into the hands-on task of creating a decorator that applies a 5% tax when the price exceeds 100, and we’ll thoroughly test its functionality.

Don’t forget to applaud 👏 if you found it helpful and share your thoughts ✉️ in the comments!

– theparthee

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top