TypeError: Class() takes no arguments in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# TypeError: Class() takes no arguments in Python

The Python "TypeError: Class() takes no arguments" occurs when we forget to define an __init__() method in a class but provide arguments when instantiating it.

To solve the error, make sure to define the __init__() (two underscores on each side) method in the class.

python typeerror class takes no arguments

shell
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 62, in <module> emp = Employee('Bobby Hadz', 100) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Class() takes no arguments TypeError: Object() takes no arguments

Here is an example of how the error occurs.

main.py
class Employee(): def get_salary(self): return self.salary # ⛔️ TypeError: Employee() takes no arguments emp = Employee('Bobby Hadz', 100) print(emp)

We haven't defined an __init__() method but are passing arguments to the class.

When a class is instantiated, its __init__() method gets called with the supplied arguments.

# Define an __init__ method to solve the error

To solve the error, make sure to define an __init__() method or correct your spelling if your class already has one.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_salary(self): return self.salary emp = Employee('Bobby Hadz', 100) print(emp.name) # 👉️ Bobby Hadz print(emp.get_salary()) # 👉️ 100

define init method to solve the error

Make sure your indentation is correct and you haven't misspelled __init__ (two underscores on each side).
main.py
def __init__(self, name, salary): # ✅ Correct def _init_(self, name, salary): # ⛔️ Incorrect def __init(self, name, salary)__: # ⛔️ Incorrect

If you mistype the __init__ method in the class's definition, it won't get invoked when the class is instantiated.

When a class defines the __init__() method, the method is invoked when an instance is created.

The following line calls the __init__() method of the class.

main.py
emp = Employee('Bobby Hadz', 100)

If you pass arguments when instantiating a class, the arguments are passed on to the __init__() method.

The __init__() method isn't supposed to return anything.

Note that the first argument the __init__() method takes is self.

main.py
def __init__(self, name, salary): self.name = name self.salary = salary

You could name this argument anything because the name self has no special meaning in Python.

self represents an instance of the class, so when we assign a variable as self.my_var = 'some value', we are declaring an instance variable - a variable unique to each instance.

When defining the __init__ method, make sure you have indented the code block consistently.

main.py
class Employee(): def __init__(self, name, salary): # ⛔️ Forgot to indent the method self.name = name self.salary = salary

If the method isn't indented correctly, you will either get an error or the method won't run when you instantiate the class.

Note that unlike other methods, the __init__ method isn't supposed to return anything.

The method is used to assign properties to the newly created class instance.

If you use a return statement in your __init__ method, you'd get an error: TypeError: __init__() should return None, not 'X'.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.