Search code examples
pythonpython-3.xabstract-factory

Takes 1 positional argument but 2 were given, self arg is provided


I am trying to execute an abstractmethod using abstract factory pattern in python but I seem to be getting and error as takes 1 positional argument but 2 were given. Any ideas what is wrong here please?

below is my sample code

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)

Error

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

Solution

  • out = cRpt.execute(kwargs)
    

    Currently you're passing kwargs as a positional argument, which execute does not accept. If you want to pass kwargs as keyword arguments, you can use:

    out = cRpt.execute(**kwargs)