2011-07-19 168 views
3

我正在解析患者访问列表(csv文件)。为了解决这个问题,我有一套自定义的类:Python - TypeError:(函数)只需要2个参数(3给出) - 但我只给了2!

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) 
     ... 

所以,我使用csv.DictReader来处理文件;内置于PtController类中。它循环通过,但对于第一个病人设定值进行以下操作:

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

错误:

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

如果我打电话initNewPt之前打印(FIRSTROW),它打印的行中的字典形式如预期。

使用python2.7,这是我第一次使用对象。思考?

回答

11

您不需要像self.initNewPt(self,firstRow)那样直接通过self,因为它会自动由Python隐式传递。

+1

啊,我明白了。然后我假设当我通过(self,otherVariable)时,python拦截并传递 - >(self,self,otherVariable) - 因此有3个参数? – chris

+2

@george是正确的。 – multipleinterfaces

5

当您拨打self.initNewPt()时,您不应将self作为参数。这是一个隐含的参数,自动出现。

4

你需要一个类的方法中调用initNewPt没有self说法:

self.initNewPt(firstRow) 
相关问题