1def our_decorator(func):
2 def function_wrapper(x):
3 print("Before calling " + func.__name__)
4 func(x)
5 print("After calling " + func.__name__)
6 return function_wrapper
7
8@our_decorator
9def foo(x):
10 print("Hi, foo has been called with " + str(x))
11
12foo("Hi")
13
1#Decorator are just function that take function as first
2#parameter and return a function
3def logging(f):
4 def decorator_function(*args, **kwargs):
5 print('executing '+f.__name__)
6 return f(*args, **kwargs)
7 return decorator_function
8#Use it like this
9@logging
10def hello_world():
11 print('Hello World')
12#calling hello_world() prints out:
13#executing hello_world
14#Hello World