Python error TypeError: __init__() takes exactly 2 arguments (1 given)

爱⌒轻易说出口 提交于 2019-12-31 03:47:25

问题


WHilst programming in Python i have come across this error about needing 2 arguments and only having one.

TypeError: __init__() takes exactly 2 arguments (1 given)

I have tried adding extra arguments and other ways but i have not found away to get it working the argument is the class self argument my code is shown below.

    import sys, pygame

pygame.init()

size = width, height = 750, 500
backgroundColour = 23, 195, 74

screen = pygame.display.set_mode((size), 0, 32)

class NPC():
    npcList = []

    def GetNPCList(self):
        listNPC = []
        for i in range(0, self.npcList):
            test = self.npcList[i].id
            listNPC.append(test)
        print(listNPC)  


def GetNPC():
    return NPC()

class NPCHandler(object):
    def __init__(self, npcId):
        self.id = id

    def newNPC(self, npcId):
        return NPCHandler(npcId)

    def addNPC(self, n = NPC):
        return n.npcList.append(n)

def GetNPCHandler():
    return NPCHandler()

def main():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                sys.exit()

        for i in range(0, 5):
            GetNPCHandler().addNPC(GetNPCHandler().newNPC(1))

        GetNPC().GetNPCList()

        screen.fill(backgroundColour)

        #pygame.draw.circle(screen, (0, 0, 0), (100, 100), 10, 0)

        pygame.display.update()

if __name__ == "__main__":
    main()

回答1:


Your NPCHandler class requires an argument (npcId), but when you create a new object inside GetNPCHandler, you are passing no arguments.

The reason the error message says you are passing one argument is that self is passed implicitly. You need to also pass the second argument (npcId).



来源:https://stackoverflow.com/questions/22736994/python-error-typeerror-init-takes-exactly-2-arguments-1-given

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!