Code Explanation:
๐น 1. Class Definition
class Test:
✅ Explanation:
A class Test is created.
Inside it:
A decorator method is defined
A normal method show() is defined
๐น 2. Decorator Function Definition
def deco(func):
✅ Explanation:
deco is a decorator function.
It takes another function (func) as argument.
๐ In this case:
func will become:
show()
๐น 3. Wrapper Function
def wrapper(self):
✅ Explanation:
wrapper is the new function that replaces original show()
self refers to current object (obj)
๐น 4. Returning Modified Output
return "Hello " + func(self)
✅ Explanation:
Calls original function:
func(self)
which is:
show(self)
๐ Original function returns:
"World"
So final result becomes:
"Hello World"
๐น 5. Returning Wrapper
return wrapper
✅ Explanation:
deco() returns wrapper
So original method show() gets replaced by wrapper
๐น 6. Applying Decorator
@deco
✅ Explanation:
This line means:
show = deco(show)
๐ So:
Original show() is passed into deco
Returned wrapper becomes new show
๐น 7. Original Method
def show(self):
return "World"
✅ Explanation:
Original method simply returns:
"World"
๐น 8. Object Creation
obj = Test()
✅ Explanation:
Creates object obj of class Test
๐น 9. Calling Decorated Method
print(obj.show())
๐ What happens internally:
Since decorator replaced method:
obj.show()
actually calls:
wrapper(obj)
Step-by-step Execution:
➤ Inside wrapper:
func(self)
calls original:
show(obj)
returns:
"World"
➤ Final result:
"Hello " + "World"
becomes:
"Hello World"
๐ฏ Final Output
Hello World

0 Comments:
Post a Comment