PythonMastery
reference 3 min read · lesson 67 of 80 in Errors

TypeError: X() takes no arguments

1 · The lesson

read

What this error means

You created an object and passed it arguments, like Point(1, 2), but the class has no __init__ method that accepts them. Without an __init__, a class can only be created with empty brackets: Point(). You almost certainly did write an __init__, so the real problem is that Python doesn't see it. Usually the name is spelled wrong, or the method is not inside the class.

When you see it

python
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    p = Point(1, 2)
TypeError: Point() takes no arguments

The name before () is your class name. The line the traceback points at is where you created the object, but the mistake is in the class definition.

Why it happens

Python only runs a method named exactly __init__: two underscores, init, two underscores. Any other spelling is just an ordinary method that nothing calls.

1. One underscore on each side. _init_ looks right in many fonts.

python
class Point:
    def _init_(self, x, y):       # one underscore each side
        self.x = x
        self.y = y

p = Point(1, 2)                   # TypeError: Point() takes no arguments

2. __int__ instead of __init__. Easy to type, and it is a real special method: Python calls __int__ when you write int(obj). So nothing looks wrong when you read it quickly.

3. Underscores missing at the end. __init (no underscores after init) is also a different name.

4. The method is not inside the class. If def __init__ is not indented under class, it is a plain function in the file, and the class is empty.

python
class Point:
    pass

def __init__(self, x, y):         # not indented: not part of Point
    self.x = x

How to fix it

Spell it __init__, with two underscores on each side, indented inside the class:

python
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = Point(1, 2)
print(p.x, p.y)                   # 1 2

Check what Python sees. vars() lists the methods actually defined in the class. If __init__ is missing from the list, the spelling or the indentation is wrong:

python
class Point:
    def _init_(self, x, y):
        self.x = x

print("__init__" in vars(Point))  # False
print("_init_" in vars(Point))    # True

Use a dataclass for classes that only store values. It writes __init__ for you, so there's nothing to misspell:

python
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

print(Point(1, 2))                # Point(x=1, y=2)

When you'd see this in real code

  • Code typed from a book or a video, where the double underscores look like one long line.
  • An editor without autocomplete, where __int__ was typed instead of __init__.
  • Code pasted from a web page or chat, where the indentation of def __init__ was lost.
  • A class that inherits from another class you wrote, where the parent's __init__ is misspelled. The child class shows the same error.

See Also

Found this useful? Share it with a friend who’s learning Python.