in Python by
What Are Decorators In Python?

1 Answer

0 votes
Python decorator gives us the ability to add new behavior to the given objects dynamically. In the example below, we’ve written a simple example to display a message pre and post the execution of a function.

def decorator_sample(func):

    def decorator_hook(*args, **kwargs):

        print("Before the function call")

        result = func(*args, **kwargs)

        print("After the function call")

        return result

    return decorator_hook


def product(x, y):

    "Function to multiply two numbers."

    return x * y

print(product(3, 3))

The output is:

Before the function call

After the function call


Related questions

0 votes
asked Jun 12, 2020 in Python by Robindeniel
0 votes
asked Jan 27, 2020 in JavaScript by AdilsonLima