chapter-13 decorators
decorators is wrap the function in inside the function
def mark(a,b):
print(a+b)
def check(funct):
def inner(a,b):
if a == -1:
a=0
return funct(a,b)
return inner
data = check(mark)
data(-1,2)
eassy way using @wrapper name to pass function
def check(funct):
def inner(a,b):
if a == -1:
a=0
return funct(a,b)
return inner
@check// decorator
def mark(a,b):
print(a-b)
mark(-1,2)
example for decoration- put a pass mark
#wrap function
def pass_mark(marks):
def para(a,b):
c=40-(a+b)
d=c/2
a,b=a+d,b+d
return marks(a,b)
return para
@pass_mark
def mark(a,b):
print("your total is: "+str(a+b))
if a+b >= 40:
print("your are pass")
else:
print("fail")
mark(16,19)
example for decoraters -// show data element and postion and total
#wrapper
def data_show(data_func):
def n_cont_par(*args,**kwargs):
total =0
for i,item in enumerate(args):
print("your "+str(i)+" item is "+str(item))
total = data_func
print("your total is: "+str(total(*args,**kwargs)))
return n_cont_par
@data_show
def data(a,b):
return a+b
data(10,10)