简体   繁体   English

在抽象 class 中编写 python 装饰器,并在继承的 class 中使用它

[英]write a python decorator inside an abstract class and use it in an inherited class

TL;DR: I want to run some logic (eg update database) inside an abstract class. TL;DR:我想在抽象的 class 中运行一些逻辑(例如更新数据库)。 maybe there is another way to do it, that's what i could think of so far (using a decorator)也许还有另一种方法可以做到这一点,这就是我到目前为止所能想到的(使用装饰器)

################################### ##################################

I have the following abstract class:我有以下抽象 class:

class MyAbstractClass(metaclass=abc.ABCMeta):

    @abc.abstractmethod
    def my_abstract_method(self, request):
        pass

    @staticmethod
    def decorator_func(self, func):
        def run_func(param_one, param_two):
            print('INSIDE run_func', param_one, param_two)
            results = self.update_database(param_one, param_two)
            func(test_id, request)
            print('FINISHED run_func')

        return run_func

    def update_database(self, param_one, param_two):
        results = <DO SOME BACKEND CALCULATIONS>
        return results

I want to create a child class that inherit from this class, and use the decorator_func in order to do some database-updates in the abstract method my_abstract_method :我想创建一个继承自此 class 的子 class,并使用decorator_func在抽象方法my_abstract_method中进行一些数据库更新:

class ChildClass(MyAbstractClass):
    def __init__(self):
        super().__init__()

    @MyAbstractClass.decorator_func
    def my_abstract_method(self, request):
        get_data()

So i know that my_abstract_method will get updated data because the decorator made sure to run update_database before.所以我知道my_abstract_method将获得更新的数据,因为装饰器确保之前运行update_database

I tried a lot of combinations (eg removing the @staticmethod or trying to apply it with @classmethod) but it won't work.我尝试了很多组合(例如删除@staticmethod 或尝试使用@classmethod 应用它),但它不起作用。 for the current code i get the error: run_func() takes 2 positional arguments but 3 were given对于当前代码,我得到错误: run_func() takes 2 positional arguments but 3 were given

Any ideas about how can i easily run update_database before the my_abstract_method will run?关于如何在my_abstract_method运行之前轻松运行update_database的任何想法?

The static method gets one argument, the function being decorated. static 方法得到一个参数,function 被修饰。 The function it returns is the one that needs a parameter to hold the implicit instance argument received by a method call.返回的 function 是需要一个参数来保存方法调用接收到的隐式实例参数的那个。

@staticmethod
def decorator_func(func):
    def run_func(self, param_one, param_two):
        print('INSIDE run_func', param_one, param_two)
        results = self.update_database(param_one, param_two)
        func(self, test_id, request)
        print('FINISHED run_func')

    return run_func

Because func is a reference to a function , not a bound method, you need to pass self explicitly as the first argument.因为func是对function的引用,而不是绑定方法,所以您需要将self作为第一个参数显式传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM