Python错误TypeError:__init __()恰好接受2个参数(给定1个) [英] Python error TypeError: __init__() takes exactly 2 arguments (1 given)
问题描述
在Python中进行Wilst编程时,我遇到了有关需要2个参数而只有1个参数的错误.
TypeError: __init__() takes exactly 2 arguments (1 given)
我尝试添加其他参数和其他方式,但是我还没有找到使它起作用的方法,该参数是类自我参数,我的代码如下所示.
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()
您的NPCHandler
类需要一个参数(npcId
),但是当您在GetNPCHandler
中创建一个新对象时,您没有传递任何参数. /p>
错误消息指出您要传递一个参数的原因是self
是隐式传递的.您还需要传递第二个参数(npcId
).
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()
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
).
这篇关于Python错误TypeError:__init __()恰好接受2个参数(给定1个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!