+1 vote
in Python by
What are Python decorators?

1 Answer

0 votes
by

Below is the decorators explanation

Decorators are functions that take another functions as argument to modify its behaviour without changing the function itself. These are useful when we want to dynamically increase the functionality of a function without changing it. Here is an example :

def smart_divide(func):

    def inner(a, b):

        print("Dividing", a, "by", b)

        if b == 0:

            print("Make sure Denominator is not zero")

            return

return func(a, b)

    return inner

@smart_divide

def divide(a, b):

    print(a/b)

divide(1,0)

Here smart_divide is a decorator function that is used to add functionality to simple divide function.

Related questions

0 votes
asked Jun 12, 2020 in Python by Robindeniel
+1 vote
asked Feb 15, 2021 in Python by SakshiSharma
...