Python Forum

Full Version: add() takes 2 positional arguments but 3 were given
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have two python files - Addition.py and test,py

Addition.py

class Addition:
    num1 = 0
    num2 = 0

    def add(n1, n2):
        num1 = n1
        num2 = n2
        return num1 + num2
test,py

import Addition

c = Addition.Addition()
print(c.add(2,3))
When I run test.py it throws an error

Error:
add() takes 2 positional arguments but 3 were given
Why am I getting this error and how to solve it?
The methods gets the 3 arguments c, 2 and 3. The solution is to add the instance in the arguments list def add(self, n1, n2): ...
Why do you need the class at all?
(Feb-08-2020, 08:42 AM)ndc85430 Wrote: [ -> ]Why do you need the class at all?

I was getting an error while creating an unofficial API. In order to analyse the error I made this test case. Thank you btw.