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.

What is TypeError?

TypeError is an error that comes when you are performing operations on an object which is invalid. It means the variables are of a different type. For example, if you have two variables one is a string and the other is an integer. If you try to sum these two numbers then you will get the TypeError. If you want to do the addition of a number with the string then you have to first convert the number to string through typecasting ( str(your_number)) and then add the existing string with it.

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 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 an 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 in 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. Please visit this generic informative article on typeerror in Python to strengthen the depth of the topic.

typeerror: exceptions must derive from baseexception – Fix Quickly

 

Thanks

Data Science Learner Team

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.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner