TypeError: Class() takes no arguments
__init__
in Python classes.For more information about argument types and parameter types, see Argument Types in Python Functions, Parameter Types in Python Functions.
What does the error message mean?
This type of error can occur when we pass some argument values to the __init__
method of a class while initializing an object.
class_instance = MyClass(<some arguments here>) TypeError: MyClass() takes no arguments
In versions of Python 3.6 and earlier, the error message may look different.
TypeError: object() takes no parameters
This error means that there is no initialization method defined in the class, or the method name is misspelled. In both cases, we cannot pass the arguments required for a new instance of the class.
If we want to pass arguments, we need to add a method and specify the parameters in that method.
If __init__
is spelled incorrectly
The most common errors in the method name are missing underscores, missing letters, or rearranged letters.
_init_
, __init_
, __int__
, __inti__
__init__
is a special method used to customize an instance of a class. If there is a typo in the name of a method, it is not treated as a special method, but as a normal method of the class.
Class with the correct __init__
method
class A: def __init__(self, x, y): self.x = x self.y = y # passing arguments without errors a = A(1, 2)
We will get an error if we forget to pass arguments to the method.
a = A()
# TypeError: A.__init__() missing 2 required positional arguments: 'x' and 'y'
Class with the incorrect _init_
method
class A: def _init_(self, x, y): self.x = x self.y = y # no missing arguments 'x' and 'y' a = A() # _init_ is a normal method a._init_(1, 2) print(a.x, a.y) # 1 2
We will get an error if we try to pass arguments to a method.
a = A(1, 2)
# TypeError: A() takes no arguments
If __init__
is not defined in the class
Because most Python 3 classes inherit methods from the base object
class, we don't get errors if our class omits an __init__
method.
class A: pass # no errors if we pass no values a = A() print(issubclass(A, object)) # True # method resolution order for class A # If __init__ is not found in class A, it is looked for in the next parent class. print(A.__mro__) # (<class '__main__.A'>, <class 'object'>)
Calling object()
returns a new featureless object. When an object.__init__
is called, it does not need to accept any arguments intended to customize an instance of the class. This raises a TypeError
. When our class does not have an __init__
method defined, it behaves just like the parent class.
>>> obj = object("value") Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> obj = object("value") TypeError: object() takes no arguments >>> class A: pass >>> a = A("value") Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> a = A("value") TypeError: A() takes no arguments
As we can see, our class uses the parent __init__
.
>>> object.__init__ is A.__init__ True
We have to redefine the __init__
method in our class with the same name.