Typeerror: takes 1 positional argument but 2 were given ( Solved )

Typeerror takes 1 positional argument but 2 were given

Typeerror: takes 1 positional argument but 2 were given is the error you get when you create a Class and call the specific method by creating an object of the class. It happens as you forget to include self parameters in the methods of the class. In this entire tutorial, you will understand how you can overcome this typeerror quickly using various ways.

Typeerror: takes 1 positional argument but 2 were given

Why this error comes?  Let’s understand it by creating a Sample Class and calling a Class method by creating an object of the it.

Execute the below lines of code to create a class.

class SampleClass:

    def fun(arg):
        print(arg)

Now let’s create an object of the class and call the “fun ” function name.

obj = SampleClass()
obj.fun("Data Science Learner")

When you will run the code you will get the fun() takes 1 positional argument but 2 were given error.

takes 1 positional argument but 2 were given Error
takes 1 positional argument but 2 were given Error

It is giving this error as you have not passed the default self parameter for the method func(). You should note that every method that is present inside the class must have a self argument. It is done to tell the interpreter that this method is the method of the class.

How to Solve this issue

To solve this ” Typeerror: takes 1 positional argument but 2 were given ” is by adding self argument for each method inside the class. It will remove the error.

Taking the same example if I will execute the below lines of code then I will not get the error.

class SampleClass:

    def fun(self,arg):
        print(arg)
obj = SampleClass()
obj.fun("Data Science Learner")

Output

Solved takes 1 positional argument but 2 were given Error
Solved takes 1 positional argument but 2 were given Error

 

Other Solution

The other way to solve this typeerror is making the method of the class to static method. This way you don’t have to add the self-argument for the method.

You have to just decorate the method with the @staticmethod above the function name.

Execute the below lines of code.

class SampleClass:

    @staticmethod
    def fun(arg):
        print(arg)
obj = SampleClass()
obj.fun("Data Science Learner")

Output

Solved takes 1 positional argument but 2 were given Error using static method
Solved takes 1 positional argument but 2 were given Error using the static method

Conclusion

Typeerror: takes 1 positional argument but 2 were given error comes mostly when you forget to add “self” argument for the method inside the class. These are the way to solve this type of Typeerror.

I hope this tutorial has solved your queries. Even if you have doubts then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner