简体   繁体   中英

Python - TypeError: (function) takes exactly 2 arguments (3 given) - but I only gave 2!

I'm parsing a list of patient visits (csv file). To deal with this, I have a custom set of classes:

class Patient:
    def __init__(self,Rx,ID):
    ....

class PtController:
    def __init__(self,openCSVFile):
        self.dict=DictReader(openCSVFile)
        self.currentPt = ''
        ....

    def initNewPt(self,row):
        Rx = row['Prescription']
        PatientID = row['PatientID']
        self.currentPt = Patient(Rx,PatientID)
        ...

So, I'm using the csv.DictReader to process the file; built into the PtController class. It iterates through, but to set values for the first patient does the following:

firstRow = self.dict.next()
self.initNewPt(self,firstRow)
    ...

The error:

TypeError: initNewPt() takes exactly 2 arguments (3 given)

If I print(firstRow) before calling initNewPt, it prints the row in dictionary form as expected.

Using python2.7, and this is my first time working with objects. Thoughts?

You do not need to pass self directly as in self.initNewPt(self,firstRow) , since it is automatically passed implicitly by Python.

When you call self.initNewPt() you should not pass self as a parameter. This is an implied parameter that's automatically present.

You need to call the initNewPt without the self argument within a class method:

self.initNewPt(firstRow)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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