How to Resolve "TypeError: object() takes no arguments" Error in Python
When creating instances of a class in Python, you might encounter the TypeError: object() takes no arguments. This error, or its more specific variations like TypeError: Human() takes no arguments, occurs when you pass arguments to a class constructor (e.g., MyClass("some_value")) but the class has not been defined with a special __init__() method to accept them.
This guide will explain the role of the __init__() constructor, show you how to reproduce the error, and provide the correct way to define your classes to accept initialization arguments, including common pitfalls to avoid.
Understanding the Error: The Role of the __init__ Constructor
In Python, when you create an instance of a class (e.g., person = Human()), a special method called a constructor is invoked to initialize the new object. This constructor method is named __init__.
If you do not define an __init__ method in your class, Python uses a default, empty constructor inherited from its base object class. This default constructor takes no arguments (other than the implicit self). The TypeError arises when you try to pass arguments to this default, argument-less constructor.
Reproducing the TypeError
Let's define a simple Human class without an __init__ method and then try to create an instance of it by passing a name.
Example of the code causing the error:
class Human:
# No __init__ method is defined here
def walk(self):
print("Walking")
try:
# We are passing "Tom", but the default constructor takes no arguments.
person = Human("Tom")
except TypeError as e:
print(f"Error: {e}")
Output:
Error: Human() takes no arguments
Depending on your Python version and the specific class, the error message might vary slightly, but the meaning is the same:
TypeError: Human() takes no argumentsTypeError: object() takes no parametersTypeError: this constructor takes no arguments
Solution: Define the __init__() Method in Your Class
To fix this, you must define the __init__() method inside your class. This method's first parameter must be self (a reference to the instance being created), followed by any other parameters you want to accept.
Solution:
class Human:
# ✅ Define the constructor to accept a 'name' argument
def __init__(self, name):
# Assign the 'name' argument to an instance attribute
self.name = name
print(f"Human object '{self.name}' created.")
def walk(self):
print(f"{self.name} is walking.")
# This now works because the __init__ method is defined to accept one argument.
person = Human("Tom")
print(f"The person's name is: {person.name}")
person.walk()
Output:
Human object 'Tom' created.
The person's name is: Tom
Tom is walking.
Common Pitfalls That Still Cause the Error
Even after adding an __init__ method, you might still encounter this or a related error due to simple mistakes. Here are the most common ones.
Mistake 1: Misspelling __init__
The constructor method must be spelled exactly __init__ with two leading and two trailing underscores (a "dunder" or "magic" method). Any typo will cause Python to miss it, and it will fall back to the default constructor.
Common typos include:
_init_(single underscores)__int__(mistypinginit)init(no underscores)
Example with a Typo:
class Robot:
# ❌ Incorrect: a typo in the constructor name
def __int__(self, version):
self.version = version
try:
# Python doesn't see a valid __init__, so it uses the default one.
r2d2 = Robot("R2-D2")
except TypeError as e:
print(f"Error: {e}")
Output:
Error: Robot() takes no arguments
Mistake 2: Forgetting the self Parameter
The first parameter of any instance method, including __init__, must be self. If you forget it, the argument you pass during instantiation will be incorrectly mapped, leading to a different but related TypeError.
Example without self:
class Dog:
# ❌ Incorrect: the 'self' parameter is missing
def __init__(name):
# This will not work as intended
self.name = name
try:
# Python tries to call __init__("Fido"), but the method only accepts one argument ('name')
fido = Dog("Fido")
except TypeError as e:
print(f"Error: {e}")
Output:
Error: Dog.__init__() takes 1 positional argument but 2 were given
This happens because the call Dog("Fido") is implicitly translated to Dog.__init__(instance_object, "Fido"). Python provides the instance as the first argument automatically, so your method signature must include self to receive it.
Mistake 3: Incorrect Indentation
Python uses indentation to define scope. If your __init__ method is not indented correctly, it will not be recognized as part of the class.
Example with Bad Indentation:
class Cat:
# ❌ Incorrect: __init__ is not indented, so it's not part of the Cat class
def __init__(self, name):
self.name = name
try:
whiskers = Cat("Whiskers")
except TypeError as e:
print(f"Error: {e}")
Output:
Traceback (most recent call last):
File "<main.py>", line 3
def __init__(self, name):
^^^
IndentationError: expected an indented block after class definition on line 1
Conclusion
The TypeError: object() takes no arguments is a clear message that there is a mismatch between the arguments you are providing when creating an object and the parameters defined in its class's __init__ constructor.
To fix this error, ensure that:
- Your class has a correctly defined
__init__()method. - The method is spelled correctly with double underscores on both sides.
- The method's first parameter is
self. - The method is indented correctly within the class block.
By validating these four points, you can ensure your class constructors are properly defined to accept arguments and initialize your objects as intended.