需要将参数传递给继承类的方法装饰器

时间:2019-08-20 15:13:09

标签: python-3.x oop inheritance pycharm python-decorators

我在pycharm中工作,并试图将参数传递给我用来装饰类方法的分页装饰器。

我创建了一个类,用于抽象API返回的给定资源的属性。我还有一个字典resource_vars,在其中跟踪api开发人员已赋予特定资源的不同变量(例如,我要赋予该资源的名称,该资源的分页限制是多少)等)。

class Resource:
    def __init__(self, resource, resource_vars):
        self.resource = resource
        self.resource_vars = resource_vars
        self.resource_name = resource_vars["name"]
        self.total_results = resource["total_results"] if "total_results" in resource else None
        self.max_items_allowed = resource_vars["items_per_page"] if "items_per_page" in resource_vars else None

然后我有第二个类,在这里我试图实现工厂设计模式,因为返回的每个Resource都有一组特定的条件和变异,在将对象插入数据库之前需要进行这些变异。

class ResourceDispatcher(Resource):  # My understanding is that i am inheriting the class Resource attributes here.

    def dispatch(self, resource, resource_name):
        dispatcher = get_dispatcher(resource_name)  # factory function defined outside the class. 
        return dispatcher(resource)

    # pycharm underlines total_results and max_items_allowed in red with 
    # warning "unresolved reference total_results"

    @paginate(pages=total_results, items_per_page=max_items_allowed)
    def dispatch_object_1(...) :
        ...

当我尝试装饰方法dispatch_object_1时,警告弹出。

我想知道是否可以使用继承类的属性将参数传递给装饰器。如果有人对我如何重构以获得此结果有任何想法,我很乐意将它们考虑在内!

0 个答案:

没有答案