How to Solve Python TypeError: object() takes no arguments

by | Programming, Python, Tips

In Python, we use __init__() as a constructor function when creating an object of a class. This function allows you to pass arguments to a class object. If you misspell the __init__ function, you will encounter the error: TypeError: object() takes no arguments.

To solve this error, you need to ensure that you spell the __init__ function with two underscores on either side of init, and you use correct indentation throughout your program.

This tutorial will go through the error in detail, and we will go through an example to learn how to solve it.


TypeError: object() takes no arguments

What is a TypeError?

TypeError occurs in Python when you perform an illegal operation for a specific data type. For example, if you try to index a floating-point number, you will raise the error: “TypeError: ‘float’ object is not subscriptable“. The part object() takes no arguments tells us that an object of the class we want to use does not accept any arguments.

What is __init__ in Python?

The __init__ method is similar to constructors in C++ and Java. We use the __init__ method to initialize the state of an object. The syntax of the __init__() function is:

def __init__(self, object_parameters):

    # Initialize the object

The function takes in self and the object parameters as input and assigns values to the data members of the class when we create an object of the class. Object parameters are the state variables that define the object. The self is a reserved keyword in Python, which represents the instance of the class.

The ‘self‘ keyword enables easy access to the class methods and parameters by other methods within the class.

When you create an object of a class, you want to put any code you want to execute at the time of object creation in the __init__ method. Let’s look at an example of a class with the __init__ method:

class Footballer

    def __init__(self, name)

        self.name = name
    

The __init__ method assigns the value for the self.name variable in the Footballer class. We can reference this variable in any following method in the class.

The syntax and spelling of the __init__ method must be correct; otherwise, you will not be able to pass arguments when declaring an object of a class. You must use two underscores on either side of init.

The “TypeError: object() takes no arguments” error can also occur due to incorrect indentation. You must use either all white spaces or all tabs to indent your code blocks in your program. For further reading on correct indentation, go to the following article: How to Solve Python IndentationError: unindent does not match any outer indentation level.

Example: Creating a Class in Python

Let’s look at an example where we create a program that stores the information of different countries. We will start by defining the class. Classes are a blueprint or a set of instructions to build a specific type of object.

class Country:

    def _init_(self, name, capital, language):

        self.name = name

        self.capital = capital

        self.language = language

    def show_main_language(self):

        print('The official language of {} is {}.'.format(self.name, self.language))

The Country class has two methods. Firstly, an __init__ method defines all of the values that objects of the class can store. The second method prints the official language of a country.

Next, we will attempt to create an object of the Country class.

bulgaria = Country("Bulgaria", "Sofia", "Bulgarian")

The above code creates an object for the country Bulgaria. Now we have an object of the class, and we can attempt to call the show_main_language() method to get the official language of Bulgaria.

bulgarian.show_main_language()

Let’s run the code to get the outcome:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
bulgaria = Country("Bulgaria", "Sofia", "Bulgarian")

TypeError: Country() takes no arguments

We throw the error because we specify values to assign to the variables inside the object, but this is only possible with a correct __init__ method definition. If there is no __init__ method present in the class, the Python interpreter does not know what to do with the values you pass as arguments during object creation.

Solution

In the example code, we declared an _init_ method, where there is one underscore on each side. The correct syntax for the constructor needs two underscores on each side. Let’s look at the revised code:

class Country:
    def __init__(self, name, capital, language):
        self.name = name
        self.capital = capital
        self.language = language

    def show_main_language(self):

        print('The official language of {} is {}.'.format(self.name, self.language))

We have a valid __init__ method in the Country class. We can create an object and call the show_main_language() method.

bulgaria = Country("Bulgaria", "Sofia", "Bulgarian")

Let’s run the program to get the result:

The official language of Bulgaria is Bulgarian.

The program successfully prints the official language of Bulgaria to the console.

Summary

Congratulations on reading to the end of this tutorial! You will encounter the error “TypeError: object() takes no arguments” when you do not declare a constructor method called __init__ in a class that accepts arguments.

To solve this error, ensure that an __init__() method is present and has the correct spelling. The method must have two underscores on either side for Python to interpret it as the constructor method.

The error can also occur if you are not using a consistent indentation method in the class that is causing the error.

For further reading on passing arguments in Python go to the article: How to Solve Python SyntaxError: positional argument follows keyword argument.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨