Python-TypeError:(函数)只接受2个参数(给定3个),但我只给出了2个!

2024-06-26 13:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在分析病人就诊列表(csv文件)。为了解决这个问题,我有一组自定义类:

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之前打印(第一行),它将按预期以字典形式打印行。

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


Tags: 文件csvselfinitdefrxdictclass
3条回答

调用self.initNewPt()时,不应将self作为参数传递。这是一个自动出现的隐含参数。

您需要在类方法中调用不带self参数的initNewPt

self.initNewPt(firstRow)

不需要像在self.initNewPt(self,firstRow)中那样直接传递self,因为它是由Python自动隐式传递的。

相关问题 更多 >