Code Explanation:
1. Defining the Decorator Function
def deco(func):
deco is a decorator function.
It receives another function (func) as its argument.
Its job is to modify or extend the behavior of that function.
2. Defining the Inner Wrapper Function
def wrapper():
return func() + 1
wrapper is an inner function that:
Calls the original function func()
Takes its result and adds 1 to it
This is how the decorator changes the behavior of func.
So instead of returning func() directly, it returns func() + 1.
3. Returning the Wrapper
return wrapper
deco returns the wrapper function.
This means the original function will be replaced by wrapper.
4. Decorating the Function f
@deco
def f():
return 10
@deco means:
f = deco(f)
So the original f is passed into deco.
deco returns wrapper.
Now f actually refers to wrapper, not the original function.
5. Calling the Decorated Function
print(f())
What happens internally:
f() actually calls wrapper().
wrapper() calls the original func() (which is original f).
Original f() returns 10.
wrapper() adds 1 → 10 + 1 = 11.
print prints 11.
6. Final Output
11
Final Answer
✔ Output:
11


0 Comments:
Post a Comment