Code Explanation:
Function Decorator Definition
def multiply(func):
return lambda x: func(x) * 3
This is a decorator named multiply.
It takes a function func as input.
It returns a new lambda function:
lambda x: func(x) * 3
→ This means it calls the original function func(x) and multiplies the result by 3.
Decorating the add Function
@multiply
def add(x):
return x + 2
The @multiply decorator wraps the add function.
So add(x) becomes:
lambda x: (x + 2) * 3
Calling the Function
print(add(5))
When add(5) is called:
First: 5 + 2 = 7
Then: 7 * 3 = 21
So the result is 21.
Final Output
21
.png)

0 Comments:
Post a Comment