0

Someone might be able to explain what is going on here. I've only started python today and it seems the constructor of my class is doing weird things. This is my constructor:

def __init__(self, studentid, fname, lname, gpa):
    self.studentid = studentid
    self.fname = fname
    self.lname = lname
    self.gpa = gpa

When I call

    student = Student(1, 2, 3, 4, 5)

it throws an error: TypeError: init() takes exactly 5 arguments (6 given)

yet when I call

student = Student(1, 2, 3, 4)

it throws this error: TypeError: init() takes exactly 5 arguments (8 given)

...?

5
  • Can you show the complete stack trace? Particularly for the Student(1, 2, 3, 4) version? Commented Apr 4, 2015 at 4:48
  • 1
    Thanks. I figured out the problem is not caused when I'm submitting the form but instead on the page redirect, cause by an error in a different function. Cheers! Commented Apr 4, 2015 at 4:53
  • 1
    your second call should work. Commented Apr 4, 2015 at 4:54
  • @CY5, can you clarify what you mean? Commented Apr 4, 2015 at 4:57
  • @CY5 I wish I could downvote your comment...that's not good advice. Commented Apr 4, 2015 at 6:11

2 Answers 2

3

This is definitely has 5 arguments

def __init__(self, studentid, fname, lname, gpa):

The correct call to initialize a Student is

student = Student(1, 2, 3, 4)

self is passed implicitly (as the object is bound by that stage). Which brings the total to 5.

Your other error will not be coming from this class. Read the traceback carefully (or post it in your question) to see where the error about 8 arguments is actually from.

Sign up to request clarification or add additional context in comments.

Comments

2

self is passed over by python implicitly. It is advised to pass keyword arguments, while calling methods and functions, for readability.

student = Student(studentid=1, fname='John', lname='Doe', gpa=4).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.