我知道这个标题对于一些古老的问题看起来很熟悉,但我看过它们中的每一个,其中没有一个能够解决。 这是我的代码:
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
请告诉我,如果我有任何错误,我很困惑。 最好的问候
答案 0 :(得分:6)
您需要添加self
作为init_animals
的第一个参数:
def init_animals(self, y):
pass
self
(类似于Java中的this
)是类的实例。每次在类中调用方法时,self
都将作为第一个参数发送给该方法。