python崩溃,错误函数接受2个参数(给定3个)

2024-10-01 09:18:37 发布

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

这在我第一次调用函数时起作用,但是第二次我收到错误消息

mloc = pygame.mouse.get_pos()
if type == "gun":
    mTowers.add(gun(mloc))

。。。在

^{pr2}$

错误是:

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

我想我要做的是将鼠标位置作为元组(和自变量)传递。很明显,它在第一次通话中起作用。有什么问题吗?在


Tags: posadd消息getiftype错误pygame
1条回答
网友
1楼 · 发布于 2024-10-01 09:18:37

self是隐式传递的,因此您实际上分别传递了tower、gun(即self)和place。在

另外,您应该在这里使用super函数,而不是显式地调用tower.__init__。在

使用super的示例(python 2.7):

class Animal(object):
  def __init__(self, nlegs=4):
    print 'in __init__ Animal'
    self.nlegs = nlegs

class Cat(Animal):
  def __init__(self, talk='meow'):
    print 'in __init__ Cat'
    super(Cat, self).__init__()
    self.talk = talk

tom = Cat()
print "I'm a cat with {} legs and I say '{}'".format(tom.nlegs, tom.talk)

相关问题 更多 >