TypeError: X() takes no arguments
1 · The lesson
readWhat 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
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.
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.
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:
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:
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:
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.
Related errors
- TypeError: takes 1 positional argument but 2 were given:
__init__exists, but it accepts fewer arguments than you passed, often becauseselfis missing. - TypeError: missing 1 required positional argument: the opposite, you passed fewer arguments than
__init__needs. - AttributeError: 'NoneType' object has no attribute 'X': another common surprise when building objects.
- IndentationError: unexpected indent: when indentation problems stop the file from running at all.
See Also
- All Python errors: the full index, by type and by when it happens.
- Classes (tutorial)
- Classes cheat sheet
- Dataclasses
Found this useful? Share it with a friend who’s learning Python.