1'pass function which we want to decorate in decorator callable object'
2def our_decorator(func): # func is function to be decorated by decorator
3
4 def wrapper(x): # x is parameter which is passed in func
5 if x%2==0:
6 return func(x)
7 else:
8 raise Exception("number should be even")
9 return wrapper
10
11@ our_decorator
12def func(x): # actual function
13 print(x,"is even")
14func(2)
15func(1)
16
17' if you do not want to use @'
18func=our_decorator(func)
19func(2)
20
21
22
23
1# This function take input from user and return
2# list of 0 and 1, zero represents odd number
3# and 1 represents even number
4def deco(func):
5 def wrap(lst):
6 x = [1 if i % 2 == 0 else 0 for i in lst]
7 return x
8 func(lst)
9 return wrap
10
11@deco
12def display(l):
13 return (l)
14
15a = [int(x) for x in input("Enter number of elements : ").split(",")]
16print(display(a))
1def deco(function):
2 def wrap(num):
3 if num % 2 == 0:
4 print(num,"is even ")
5 else:
6 print(num,"is odd")
7 function(num)
8 return wrap
9
10@deco
11def display(num):
12 return num
13display(9) # pass any number to check whether number is even or odd
1#This function take user input for list and returns a tuple cotaining 2 lists
2#one of even numbers another of odd numbers
3def deco(function):
4 def wrap(lst):
5 even = []
6 odd = []
7 for i in lst:
8 if i % 2 == 0:
9 even.append(i)
10 else:
11 odd.append(i)
12 return even,odd
13 function(lst)
14 return wrap
15
16@deco
17def display(lst):
18 print(lst)
19
20
21a = [int(x) for x in input("Enter number of elements : ").split(",")]
22print(display(a))
23
1# this functon converts any string into uppercase
2def deco(function):
3 def wrap(s):
4 return s.upper()
5 function(s)
6 return wrap
7
8@deco
9def display(s):
10 return s
11print(display("not bad"))
1def deco(func):
2 def wrap(s):
3 d = {}
4 for i in s:
5 d[i] = s.count(i)
6 func(s)
7 return d
8 return wrap
9
10@deco
11def count(stng):
12 return stng
13mystring = "scorpio"
14print(count(mystring))