Code Explanation:
๐น 1. Decorator Function Definition
def deco(func):
✅ Explanation:
A function deco is created.
It takes another function (func) as argument.
๐ In this code:
func will become:
show()
๐น 2. Wrapper Function Definition
def wrapper():
✅ Explanation:
wrapper is an inner function.
This function will replace original show().
๐น 3. Modified Return Value
return "Hi " + func()
✅ Explanation:
Calls original function:
func()
๐ Original function returns:
"Python"
Then:
"Hi " + "Python"
becomes:
"Hi Python"
๐น 4. Returning Wrapper
return wrapper
✅ Explanation:
Decorator returns wrapper function.
So original function gets replaced.
๐น 5. Applying Decorator
@deco
✅ Explanation:
This is shortcut for:
show = deco(show)
๐ What happens:
Original show() passed into deco
wrapper returned
show now points to wrapper
๐น 6. Original Function
def show():
return "Python"
✅ Explanation:
Original function returns:
"Python"
But now it is wrapped by decorator.
๐น 7. Calling Function
print(show())
๐ What actually runs:
wrapper()
๐น 8. Execution Inside Wrapper
Step 1:
func()
calls original:
show()
returns:
"Python"
Step 2:
Wrapper adds:
"Hi " + "Python"
Result:
"Hi Python"
๐ฏ Final Output
Hi Python

0 Comments:
Post a Comment