Code Explanation:
๐น 1. Defining the Decorator Function
def deco(func):
๐ This defines a decorator function named deco
๐ It takes another function func as input
๐น 2. Creating the Wrapper Function
def wrapper():
๐ Inside deco, we define a nested function called wrapper
๐ This function will modify or extend the behavior of func
๐น 3. Calling Original Function + Modifying Output
return func() + 1
๐ func() → calls the original function
๐ + 1 → adds 1 to its result
๐ก So this decorator increases the return value by 1
๐น 4. Returning the Wrapper
return wrapper
๐ Instead of returning the original function,
๐ we return the modified version (wrapper)
๐น 5. Applying the Decorator
@deco
๐ This is syntactic sugar for:
f = deco(f)
๐ It means:
pass function f into deco
replace f with wrapper
๐น 6. Defining the Original Function
def f():
return 5
๐ This function simply returns 5
๐น 7. Calling the Function
print(f())
๐ Actually calls wrapper() (not original f)
๐ Inside wrapper:
func() → returns 5
+1 → becomes 6
✅ Final Output
6

0 Comments:
Post a Comment