接受1个位置参数,但给出了2个,提供了自arg

时间:2019-06-02 21:39:30

标签: python python-3.x abstract-factory

我正在尝试使用python中的抽象工厂模式执行抽象方法,但是我似乎越来越错误,因为需要1个位置参数,但是给出了2个。有什么想法请问这里有什么问题吗?

下面是我的示例代码

Report.py

from abc import ABCMeta, abstractmethod

class Report(metaclass=ABCMeta):
    def __init__(self, name=None):
        if name:
            self.name = name

    @abstractmethod
    def execute(self, **arg):
        pass
ChildReport.py

import json
from Report import Report


class CReport(Report):
    def __init__(self, name=None):
        Report.__init__(self, name)

    def execute(self, **kwargs):
        test = kwargs.get('test')
ReportFactory.py

from ChildReport import CReport

class ReportFactory(object):

    @staticmethod
    def getInstance(reportType):
        if reportType == 'R1':
            return CReport()
testReport.py

from ReportFactory import ReportFactory

cRpt = ReportFactory.getInstance('R1')
kwargs = {'test' :'test'}
out = cRpt.execute(kwargs)

错误

    out = cRpt.execute(kwargs)
TypeError: execute() takes 1 positional argument but 2 were given

1 个答案:

答案 0 :(得分:1)

out = cRpt.execute(kwargs)

当前,您将kwargs作为位置参数传递,execute不接受。如果要将kwargs作为关键字参数传递,可以使用:

out = cRpt.execute(**kwargs)