name decorator in python

Solutions on MaxInterview for name decorator in python by the best coders in the world

showing results for - "name decorator in python"
Nicolás
09 Apr 2017
1# defining a decorator  
2def hello_decorator(func):  
3    
4    # inner1 is a Wrapper function in   
5    # which the argument is called  
6        
7    # inner function can access the outer local  
8    # functions like in this case "func"  
9    def inner1():  
10        print("Hello, this is before function execution")  
11    
12        # calling the actual function now  
13        # inside the wrapper function.  
14        func()  
15    
16        print("This is after function execution")  
17            
18    return inner1
19
20# defining a function, to be called inside wrapper  
21def function_to_be_used():  
22    print("This is inside the function !!")  
23    
24    
25# passing 'function_to_be_used' inside the  
26# decorator to control its behavior  
27function_to_be_used = hello_decorator(function_to_be_used)  
28    
29    
30# calling the function  
31function_to_be_used()  
32Output:
33
34Hello, this is before function execution
35This is inside the function !!
36This is after function execution
37