TypeError: object() takes no parameters in Python Custom Classes
Fix typos in __init__ constructor definition (ensure double underscores on both sides: def __init__(self, ...):).
Root Cause Analysis
This error occurs when Python tries to instantiate a custom class with arguments (MyClass(arg1, arg2)), but the class's __init__ constructor was defined with a typo (such as def _init_(self): with single underscores or def __init(self):), causing Python to fall back to the default object.__init__() which takes zero parameters.
1. Single Underscore Typos in init
The dunder method for class initialization requires exactly two leading and two trailing underscores: __init__. If typed as def _init_(self, name):, Python treats it as a normal custom method and leaves the default object.__init__ in place. When calling MyClass('Alice'), Python passes 'Alice' to object.__init__(), raising TypeError: object() takes no parameters.
2. Misspelling init as int or ini
Accidentally typing def __int__(self): (which defines integer type casting) instead of __init__.
3. Overriding new Without Matching init Signature
Custom __new__(cls, *args, **kwargs) methods that do not consume or forward arguments properly before delegating to super().__new__().
4. Indentation Mistakes Placing init Outside Class Body
Indenting def __init__ at the module level rather than inside the class block.
Reproduction Code (MCVE)
# Simulating typo in __init__ constructor (single underscore)
class UserAccount:
def _init_(self, username, email): # Bug: single underscores
self.username = username
self.email = email
# Passing arguments to fallback object.__init__ raises TypeError
u = UserAccount('alice', 'alice@example.com')
Solution 1: Use Double Underscores for __init__
Ensure the constructor method is spelled with two underscores on each side: def __init__(self, ...):.
class UserAccount:
# Correct dunder constructor with double underscores
def __init__(self, username, email):
self.username = username
self.email = email
user = UserAccount('alice', 'alice@example.com')
print(f'User initialized successfully: {user.username} <{user.email}>')
Solution 2: Use @dataclass for Automatic Constructor Generation
Use @dataclasses.dataclass to have Python generate type-safe __init__ constructors automatically.
from dataclasses import dataclass
@dataclass
class UserAccount:
username: str
email: str
user = UserAccount('bob', 'bob@example.com')
print(f'Dataclass user: {user}')
A common mistake is typing __init__ inside a text editor without a monospace font, where double underscores (__) look like a single long underscore (_). Always check with your linter (flake8 / ruff / pyright) which flags unused _init_ methods immediately. Edge cases occur when inheriting from immutable types (like tuple or int): immutable types require overriding __new__ rather than __init__. Contrast this error with TypeError: __init__() takes 1 positional argument but 2 were given, which occurs when __init__ exists but lacks parameter declarations.