【问题标题】:TypeError: init_animals() takes 1 positional arguments but 2 were given [duplicate]TypeError:init_animals()采用1个位置参数,但给出了2个[重复]
【发布时间】:2013-10-28 09:01:37
【问题描述】:

我知道这个标题对于一些老问题来说看起来很熟悉,但我已经查看了每一个问题,但没有一个能解决。 这是我的代码:

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

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

【问题讨论】:

  • def init_animals(self, y):

标签: python class methods typeerror init


【解决方案1】:

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

def init_animals(self, y):
    pass

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

【讨论】:

  • 很抱歉犯了这样的错误,非常感谢
猜你喜欢
  • 2020-05-20
  • 1970-01-01
  • 2017-10-26
  • 2018-12-29
  • 2023-01-11
  • 2018-12-06
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多