Search code examples
pythoninheritancesuper

Python 2.7 __init__() takes exactly 2 arguments (3 given)


I've got these classes. Person is the parent class and Student is the child class:

class Person(object):
    def __init__(self, name):
        self.name = name

class Student(Person):
    def __init__(self, avr, name):
        self.avr = avr
        super(Student, self).__init__(self, name)

I get this error when I try to make an instance of Student:

__init__() takes exactly 2 arguments (3 given)

What is wrong with my code?


Solution

  • If you are using super, you don't pass self to the target method. It is passed implicitly.

    super(Student, self).__init__(name)
    

    That's 2 arguments total (self, name). When you passed self, that was 3 total (self, self, name).