This question already has an answer here:
这个问题已经有了答案:
- TypeError: method() takes 1 positional argument but 2 were given 4 answers
- 类型错误:方法()接受1个位置参数,但2得到4个答案。
I know this title look familiar to some old questions, but i've looked at every single one of them, none of them solves. And here is my codes:
我知道这个题目看起来很熟悉一些老问题,但我已经看过了其中的每一个,没有一个能解决。这是我的代码:
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)
However, i got the following error:
但是,我有以下错误:
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
Please tell me if i got any mistakes, im so confused by this. Best regards
如果我有什么错误,请告诉我,我很困惑。致以最亲切的问候
1 个解决方案
#1
6
You need to add self
as the first argument of init_animals
:
你需要把self作为init_animals的第一个参数:
def init_animals(self, y):
pass
self
(similar to this
in Java) is the instance of the class. Every time you call a method within the class, self
is sent to that method as the first argument.
self(类似于Java中的这个)是类的实例。每次在类中调用方法时,self被发送到该方法作为第一个参数。
#1
6
You need to add self
as the first argument of init_animals
:
你需要把self作为init_animals的第一个参数:
def init_animals(self, y):
pass
self
(similar to this
in Java) is the instance of the class. Every time you call a method within the class, self
is sent to that method as the first argument.
self(类似于Java中的这个)是类的实例。每次在类中调用方法时,self被发送到该方法作为第一个参数。