Skip to content

How to Resolve "TypeError: Class() takes no arguments" in Python

Fix the Python error when instantiating a class with arguments but no matching __init__() method by properly defining, spelling, and indenting the constructor.

The TypeError: Class() takes no arguments (or TypeError: Object() takes no arguments) is a common error in Python's object-oriented programming. It typically occurs when you try to pass arguments while creating an instance of a class that doesn't have an __init__() method defined to accept them, or when the __init__() method is misspelled or incorrectly indented.

This guide explains the cause of this error and provides clear solutions.

Understanding the Error: The Role of __init__()

When you create an instance of a class in Python (e.g., emp = Employee('Tom Nolan', 100)), Python implicitly calls the class's __init__() method (the constructor). This method is responsible for initializing the new object's state, often by accepting arguments and assigning them to instance attributes.

The TypeError: Class() takes no arguments arises because you are providing arguments during instantiation, but the class either:

  • Lacks an __init__() method entirely.
  • Has an __init__() method defined, but it doesn't accept any arguments (other than self).
  • Has a typo in the __init__ method name (e.g., _init_, __init).
  • Has incorrect indentation for the __init__ method.

Solution 1: Define the __init__() Method

The primary solution is to define (or correctly define) the __init__() method within your class to accept the arguments you intend to pass during instantiation.

class Employee():
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def get_salary(self):
        return self.salary

emp = Employee('Tom Nolan', 100)
print(emp.name)          # Output: Tom Nolan
print(emp.get_salary())  # Output: 100
  • The __init__ method now correctly accepts name and salary arguments (in addition to the mandatory self).

Solution 2: Check for Typos in __init__()

Ensure you haven't misspelled __init__. It must have two leading underscores and two trailing underscores.

def __init__(self, arg1):

Solution 3: Correct Indentation

The __init__ method, like all methods within a class, must be correctly indented.

class Employee():
    def __init__(self, name):
        self.name = name

Solution 4: Instantiate Without Arguments

If your class truly doesn't need any arguments during initialization (i.e., it has no __init__ or an __init__(self) with only self), then simply instantiate it without passing any arguments:

class SimpleClass():
    message = "Hello"

    def greet(self):
        print(self.message)

instance = SimpleClass()
instance.greet() # Output: Hello

Conclusion

The TypeError: Class() takes no arguments usually points to a mismatch between the arguments provided during class instantiation and the parameters defined in the class's __init__() method.

By ensuring your class has a correctly defined, spelled, and indented __init__ method that accepts the necessary arguments, you can effectively resolve this common Python error. If no arguments are needed, instantiate the class without providing any.

Navigation