Python Forum
add() takes 2 positional arguments but 3 were given - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: add() takes 2 positional arguments but 3 were given (/thread-24314.html)



add() takes 2 positional arguments but 3 were given - Man_from_India - Feb-08-2020

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?


RE: add() takes 2 positional arguments but 3 were given - Gribouillis - Feb-08-2020

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): ...


RE: add() takes 2 positional arguments but 3 were given - ndc85430 - Feb-08-2020

Why do you need the class at all?


RE: add() takes 2 positional arguments but 3 were given - Man_from_India - Feb-10-2020

(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.