# defining a decorator def hello_decorator(func): # inner1 is a Wrapper function in # which the argument is called # inner function can access the outer local # functions like in this case "func" def inner1(): print("Hello, this is before function execution") # calling the actual function now # inside the wrapper function. func() print("This is after function execution") return inner1 # defining a function, to be called inside wrapper def function_to_be_used(): print("This is inside the function !!") def uppercase(func): def upper_wrapper(): original_result = func() modified_result = original_result.upper() print(modified_result) return upper_wrapper def title(func): def title_wrapper(): original_result = func() modified_result = original_result.title() print(modified_result) return title_wrapper def swappingcase(func): def swappingcase_wrapper(): original_result = func() modified_result = original_result.swapcase() print(modified_result) return swappingcase_wrapper def lowercase(func): def lower_wrapper(): original_result = func() modified_result = original_result.lower() print(modified_result) return lower_wrapper def replace(func): def replace_wrapper(): original_result = func() modified_result = original_result.replace(original_result, 'Goodbye') print(modified_result) return replace_wrapper @uppercase def greet_upper(): print("Hello change to uppercase") return 'Hello' @lowercase def greet_lower(): print('Hello change to lowercase') return 'Hello' @replace def greet_replace(): print('Hello replace with Goodbye') return 'Hello' @title def greet_title(): print('hello and goodbye replaced with title caps') return 'hello and goodbye replaced with title caps' @swappingcase def greet_swappingcase(): print('hElLo aNd gOoDbYe RePlACeD wItH sWaPcAsE') return 'hElLo aNd gOoDbYe RePlACeD wItH sWaPcAsE' function_to_be_used = hello_decorator(function_to_be_used) function_to_be_used() print("\n") decorated_greet_upper = uppercase(greet_upper) greet_upper() print("\n") decorated_greet_lower = lowercase(greet_lower) greet_lower() print("\n") decorated_greet_replace = replace(greet_replace) greet_replace() print("\n") decorated_greet_title = title(greet_title) greet_title() print("\n") decorated_greet_swappingcase = swappingcase(greet_swappingcase) greet_swappingcase()
Hello, this is before function execution This is inside the function !! This is after function execution Hello change to uppercase HELLO Hello change to lowercase hello Hello replace with Goodbye Goodbye hello and goodbye replaced with title caps Hello And Goodbye Replaced With Title Caps hElLo aNd gOoDbYe RePlACeD wItH sWaPcAsE HeLlO AnD GoOdByE rEpLacEd WiTh SwApCaSe