Code Explanation:
๐น 1. Class Definition
class Test:
✅ Explanation:
A class named Test is created.
Inside this class:
A decorator function deco is defined
A 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 code:
func will be:
show()
๐น 3. Wrapper Function Inside Decorator
def wrapper(self):
✅ Explanation:
wrapper is a new function created inside decorator.
This function will replace original show() method.
self refers to current object (obj).
๐น 4. Modified Return Statement
return "Hello " + func(self)
✅ Explanation:
Calls original function:
func(self)
๐ Original show() returns:
"World"
So final result becomes:
"Hello World"
๐น 5. Returning Wrapper
return wrapper
✅ Explanation:
Decorator returns wrapper.
So original function is replaced by wrapper function.
๐น 6. Applying Decorator
@deco
✅ Explanation:
This line means:
show = deco(show)
๐ What happens:
Original show() is passed into deco
deco returns wrapper
show now points to wrapper
๐น 7. Original Method
def show(self):
return "World"
✅ Explanation:
Original method simply returns:
"World"
But because of decorator,
this method is wrapped inside wrapper.
๐น 8. Object Creation
obj = Test()
✅ Explanation:
Creates object obj of class Test.
๐น 9. Calling Method
print(obj.show())
๐ What happens internally:
Because of decorator:
obj.show()
actually becomes:
wrapper(obj)
๐น 10. Execution Inside Wrapper
Step 1:
func(self)
calls original:
show(obj)
returns:
"World"
Step 2:
Wrapper adds:
"Hello " + "World"
Result:
"Hello World"
๐ฏ Final Output
Hello World

0 Comments:
Post a Comment