Skip to content
Related Articles
Open in App
Not now

Related Articles

Creating Decorator inside a class in Python

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 05 Sep, 2020
Improve Article
Save Article

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.


# creating class A
class A :
    def Decorators(func) :
        def inner(self) :
            print('Decoration started.')
            print('Decoration of function completed.\n')
        return inner
    def fun1(self) :
        print('Decorating - Class A methods.')
# creating class B
class B(A) :
    def fun2(self) :
        print('Decoration - Class B methods.')
obj = B()


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.


class Check_no :
    # decorator function
    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
    #instance method
    def is_even(self, no) :            
        print('User Input : ', no)
obj = Check_no()


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.


# parent class
class Student :            
    # decorator function
    def decor(func) :                
        def grade(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
# child class
class Result(Student) :            
    # instance method
    def result(self,marks) :            
        print('Your Score : ',marks)
# creating object of parent class
obj = Result()        


Your Score :  89
Grade : A
Your Score :  34
Grade : F

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!