Fixing Python TypeError: object() takes no parameters

Python TypeError: object() takes no parameters is an error which occurs when you override both new and init, while passing other extra arguments to the object methods.

In the blog post I attempt to explain why this error takes place and how you can solve it, I will also add other solutions that could solve the error if possible.

Exploring the Python TypeError: object() takes no parameters

Python TypeError: object() takes no parameters is an error which occurs when you pass other extra arguments to the object methods while overriding both new and init at the same time.

Your error message should match the one bellow. We want to avoid confusion by not mixing between error messages.

                                                                       #
TypeError: object() takes no parameters
                                                                       #

To solve the problem above, I have 2 solutions which worked for me, bellow is a detailed explanation of both.

The Method which Solved my problem: Do not override new and init, and pass extra arguments at the same time

The error is happening because basically you are not allowed to pass extra arguments to object methods while overriding both init and new , why ? I will explain why later in this article, all you have to know now is that this is not impossible to solve, this error is solvable with little intervention.

You cannot override both init and new at the same time but you can override one of them which allows you to pass extra arguments to the other method. All you have to do is to write your method like in the example bellow.

                                                                       #
def __new__(age, weight, height):
    print("calling method __new__  in class {}".format(age))
    return object.__new__(age)
                                                                       #

The error will happen if you pass an extra argument like this, so avoid doing it.

                                                                       #
def __new__(age, weight, height):
    print("calling method __new__  in class {}".format(age))
    return object.__new__(age, weight, height)
                                                                       #

The solution above should be enough to solve the problem.

Now, why is it not allowed to do what you wanted to do in the first place? The answer was provided by the members of the official cpython GitHub and it goes like this.

” When neither is overridden, we want to hear complaints about excess arguments, since their presence could indicate there’s a bug. When defining an Immutable type, we are likely to override only
new(), . Since new() defines the signature for the type, it would be a pain to have to override init() just to
stop it from complaining. “

Summing-up : 

This article is over guys, I hope the error : Python TypeError object() takes no parameters is gone.

You can support us by donating to our Kofi account, you can find a red button at the top of this page. Good luck with your Python journey, keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/