What is @try 🤯 before this function in Python?

what-is-@try-before-this-function-in-python?

History ⏳

I was looking into the code base of a library few years back ⌚, and I saw something strange 🤯. It goes like –

@try
def fetch(url: str):
    resp = request.get(url)
    return resp.json()

So the definition of fetch function is clear enough, but what the hack is this @try 🤯 just before the function definition?

That was a Decorator 🤔

Decorators are very powerful and useful tool in Python as it allows programmers to modify the behaviour of a function. Decorators allow us to wrap another function in order to extend the behaviour of the wrapped function.

Scary Definition 😨, Let’s understand with examples

Example 1: Functions as an object 🤔

def upper_text(text):
    return text.upper()

print(upper_text('Hello'))

func = upper_text

print(func('Hello'))
HELLO
HELLO

Example 2: Function as an argument 🤔

def upper_text(text):
    return text.upper()

def lower_text(text):
    return text.lower()

def func(func):
    # storing the function in a variable
    msg = func("Hello")

    print(msg)

func(upper_text)
func(lower_text)
HELLO
hello

Example 3: Returning function 🤔

def create_adder(x):
    def adder(y):
        return x + y

    return adder

add_15 = create_adder(15)

print(add_15(10))
25

Example 4: Decorator 🚀

# importing libraries
import time
import math

# decorator to calculate duration
# taken by any function.
def calculate_time(func):

    # added arguments inside the inner1,
    # if function takes any arguments,
    # can be added like this.
    def inner1(*args, **kwargs):

        # storing time before function execution
        begin = time.time()

        func(*args, **kwargs)

        # storing time after function execution
        end = time.time()
        print("Total time taken in : ", func.__name__, end - begin)

    return inner1

# this can be added to any function present,
# in this case to calculate a factorial
@calculate_time
def factorial(num):

    # sleep 2 seconds because it takes very less time
    # so that you can see the actual difference
    time.sleep(2)
    print(math.factorial(num))

# calling the function.
factorial(10)
3628800
Total time taken in :  factorial 2.0061802864074707

Back to History 😃

So, what was that?

@try
def fetch(url: str):
    resp = request.get(url)
    return resp.json()

The definition of try was like this –

def try(func):
    def exe(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            return {
                "message": "Error while processing the request",
                "details": str(e)
            }
    return exe

Hit Like ❤️ and share your thoughts in the comments 💬

Thank you

cheers

Total
1
Shares
Leave a Reply

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

Previous Post
react-router-version-6-tutorial-how-to-set-up-react-router@6

React Router Version 6 Tutorial How to Set up React Router@6

Next Post
project-assumptions:-a-quick-guide

Project Assumptions: A Quick Guide

Related Posts