+ 2
Decorators are tools mainly used to modify the behavior of a function in python. Decorators do so by wrapping a function in another function. The outer function is called the decorator, which takes a function as an argument and returns a modified version. For Example: def make_pretty(func): # define the inner function def inner(): # add some additional behavior to decorated function print("I got decorated") # call original function func() # return the inner function return inner # define ordinary function def ordinary(): print("I am ordinary") # decorate the ordinary function decorated_func = make_pretty(ordinary) # call the decorated function decorated_func() ==> And the same above function can be written as: def make_pretty(func): def inner(): print("I got decorated") func() return inner @make_pretty def ordinary(): print("I am ordinary") ordinary()
26th Dec 2022, 5:59 PM
UnknownX
UnknownX - avatar
+ 1
It is a complicated topic unless you are very clear of functions and classes. I too felt that it has not much explanation and good examples.
26th Dec 2022, 4:58 PM
Neethu Nath
Neethu Nath - avatar