Home > Software engineering >  Python - TypeError: generateID() takes 3 positional arguments but 4 were given
Python - TypeError: generateID() takes 3 positional arguments but 4 were given

Time:12-15

My script contains 4 classes and 1 interface. I have a method generateID which generate an ID according to outputmode, a mode (modeCB) and a dataframe (data_h_f). When I execute my class I have this error :

TypeError: generateID() takes 3 positional arguments but 4 were given

I'm beginner in python especially objected oriented programming, I don't know if the design of my script is good. I have 3 arguments for my method generateID() and 3 parameters everywere.

class OutputMode(object):
    def __init__(self,name,startTime,intervalSeconds,timezone):
    ...

class IDCalculation_I:
    def generateID(outputMode,modeCB,data_h_df):
        pass

class IDCase1(IDCalculation_I):
    def generateID(outputMode,modeCB,data_h_df):
    ...

class Fingerprinter(object):
    def __init__(self,outputMode,modeCB=CONST_MODE_CONT):
        self._modeCB     = modeCB
        self._outputMode = outputMode

    def generateID(outputMode,modeCB,data_h_df):
        pass

    def run(self):
        return self.generateID(outputMode,modeCB,data_h_df)

def main():
    outputMode = OutputMode('EEA','06:00',8*3600,pytz.timezone('Europe/Paris'))
    f1 = Fingerprinter(outputMode, CONST_MODE_CONT)
    t = f1.generateID(outputMode,CONST_MODE_CONT,data_h_df)

if __name__ == '__main__':
    main()

CodePudding user response:

class IDCalculation_I:
    def generateID(self,outputMode,modeCB,data_h_df):
        pass

class IDCase1(IDCalculation_I):
    def generateID(self, outputMode,modeCB,data_h_df):
    ...

class Fingerprinter(object):
    def __init__(self,outputMode,modeCB=CONST_MODE_CONT):
        self._modeCB     = modeCB
        self._outputMode = outputMode

    def generateID(self,outputMode,modeCB,data_h_df):
        pass

    def run(self):
        return self.generateID(outputMode,modeCB,data_h_df)

You forgot to add self. Or use @staticmethod decorator if you don't want to add self.

class IDCalculation_I:
    @staticmethod
    def generateID(outputMode,modeCB,data_h_df):
        pass

class IDCase1(IDCalculation_I):
    @staticmethod
    def generateID(outputMode,modeCB,data_h_df):
    ...

class Fingerprinter(object):
    def __init__(self,outputMode,modeCB=CONST_MODE_CONT):
        self._modeCB     = modeCB
        self._outputMode = outputMode

    @staticmethod
    def generateID(outputMode,modeCB,data_h_df):
        pass

    def run(self):
        return self.generateID(outputMode,modeCB,data_h_df)
  • Related