Creating Decorator inside a class in Python
Last Updated :
05 Sep, 2020
We can easily create decorators inside a class and it is easily accessible for its child classes. During Decorator creation, we must take care that the function that we are defining inside the decorator must take current object reference (self) as a parameter, and while we are accessing that decorator from child class that time we must call that decorator using the class name(class in which Decorator is present).
Example 1: Here in this example we are creating a decorator function inside Class A. Inside Class A “fun1” Instance Method is calling the decorator function “Decorators” inside Class B “fun2”. Instance Method is calling the decorator function of Class A. To use the decorator of Class A, we must require using Class name in which decorator is present that’s why we use “@A.Decorators” here.
Python3
class A :
def Decorators(func) :
def inner( self ) :
print ( 'Decoration started.' )
func( self )
print ( 'Decoration of function completed.\n' )
return inner
@Decorators
def fun1( self ) :
print ( 'Decorating - Class A methods.' )
class B(A) :
@A .Decorators
def fun2( self ) :
print ( 'Decoration - Class B methods.' )
obj = B()
obj.fun1()
obj.fun2()
|
Output:
Decoration started.
Decorating - Class A methods.
Decoration of function completed.
Decoration started.
Decoration - Class B methods.
Decoration of function completed.
Example 2: Checking number is Even or Odd using Decorator.
Python3
class Check_no :
def decor(func) :
def check( self , no) :
func( self , no)
if no % 2 = = 0 :
print ( 'Yes, it\'s EVEN Number.' )
else :
print ( 'No, it\'s ODD Number.' )
return check
@decor
def is_even( self , no) :
print ( 'User Input : ' , no)
obj = Check_no()
obj.is_even( 45 )
obj.is_even( 2 )
obj.is_even( 7 )
|
Output:
User Input : 45
No, it's ODD Number.
User Input : 2
Yes, it's EVEN Number.
User Input : 7
No, it's ODD Number.
Example 3: Checking Grade from Marks.
Python3
class Student :
def decor(func) :
def grade( self ,marks) :
func( self ,marks)
if marks < 35 :
print ( 'Grade : F' )
elif marks < 50 :
print ( 'Grade : E' )
elif marks < 60 :
print ( 'Grade : D' )
elif marks < 70 :
print ( 'Grade : C' )
elif marks < 80 :
print ( 'Grade : B' )
elif marks < 100 :
print ( 'Grade : A' )
return grade
class Result(Student) :
@Student .decor
def result( self ,marks) :
print ( 'Your Score : ' ,marks)
obj = Result()
obj.result( 89 )
obj.result( 34 )
|
Output:
Your Score : 89
Grade : A
Your Score : 34
Grade : F
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...