TypeError:init_animals()接受1个位置参数,但给出2个

时间:2013-10-19 15:50:09

标签: python class methods typeerror init

我知道这个标题对于一些古老的问题看起来很熟悉,但我看过它们中的每一个,其中没有一个能够解决。 这是我的代码:

class Island (object):E,W,R,P
  def __init__(self, x, y):
    self.init_animals(y)
  def init_animals(y):
    pass

isle = Island(x,y)

然而,我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
TypeError: init_animals() takes 1 positional arguments but 2 were given

请告诉我,如果我有任何错误,我很困惑。 最好的问候

1 个答案:

答案 0 :(得分:6)

您需要添加self作为init_animals的第一个参数:

def init_animals(self, y):
    pass

self(类似于Java中的this)是类的实例。每次在类中调用方法时,self都将作为第一个参数发送给该方法。