Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: init_animals() takes 1 positional arguments but 2 were given [duplicate]

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

like image 654
libra Avatar asked Sep 04 '25 17:09

libra


1 Answers

You need to add self as the first argument of 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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!