📜  在Python中的类中创建装饰器

📅  最后修改于: 2022-05-13 01:55:38.858000             🧑  作者: Mango

在Python中的类中创建装饰器

我们可以很容易地在一个类中创建装饰器,并且它的子类可以很容易地访问它。在装饰器创建期间,我们必须注意我们在装饰器中定义的函数必须将当前对象引用(self)作为参数,并且当我们从子类访问该装饰器时,我们必须使用该类调用该装饰器名称(装饰器所在的类)。

示例 1:在此示例中,我们在 A 类中创建一个装饰器函数。在 A 类“fun1”实例方法中调用 B 类“fun2”中的装饰器函数“装饰器”。实例方法调用A类的装饰器函数。要使用A类的装饰器,我们必须要求使用装饰器所在的类名,这就是我们在这里使用“@A.Decorators”的原因。

Python3
# creating class A
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.')
  
# creating class B
class B(A) :
    @A.Decorators
    def fun2(self) :
        print('Decoration - Class B methods.')
  
obj = B()
obj.fun1()
obj.fun2()


Python3
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
  
    @decor
      
    #instance method
    def is_even(self, no) :            
        print('User Input : ', no)
  
obj = Check_no()
obj.is_even(45)
obj.is_even(2)
obj.is_even(7)


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


输出:

Decoration started.
Decorating - Class A methods.
Decoration of function completed.

Decoration started.
Decoration - Class B methods.
Decoration of function completed.

示例 2:使用装饰器检查数字是偶数还是奇数。

Python3

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
  
    @decor
      
    #instance method
    def is_even(self, no) :            
        print('User Input : ', no)
  
obj = Check_no()
obj.is_even(45)
obj.is_even(2)
obj.is_even(7)

输出:

User Input :  45
No, it's ODD Number.
User Input :  2
Yes, it's EVEN Number.
User Input :  7
No, it's ODD Number.

示例 3:根据分数检查成绩。

Python3

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

输出:

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