0% found this document useful (0 votes)
4 views2 pages

Decorator

The document explains the concept of decorators in Python, which are functions that wrap other functions to modify their behavior. It provides examples of using decorators to handle specific cases, such as replacing a value and calculating total marks. Additionally, it demonstrates how to display data elements and their positions using a decorator.

Uploaded by

dhaniyavel12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Decorator

The document explains the concept of decorators in Python, which are functions that wrap other functions to modify their behavior. It provides examples of using decorators to handle specific cases, such as replacing a value and calculating total marks. Additionally, it demonstrates how to display data elements and their positions using a decorator.

Uploaded by

dhaniyavel12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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)

You might also like