Decorators in python
Decorators in python
decorator to change the behavior of the existing function
A decorator in Python is a function that takes another function as its argument, and returns yet another function .
Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.
def division(a,b):
if a<b:
a,b = b,a
print(a/b)
division(2,4)
2.0Using Decorators
def division(a, b):
print(a / b)
def smart_divison(func):
def inner_fun(a, b):
if a < b:
a, b = b, a
return func(a, b)
return inner_fun
new = smart_divison(division)
new(2, 4)
2.0
Post a Comment