Last updated: Apr 8, 2024
Reading time·3 min
The Python "TypeError: Class() takes no arguments" occurs when we forget to
define an __init__()
method in a class but provide arguments when
instantiating it.
To solve the error, make sure to define the __init__()
(two underscores on
each side) method in the class.
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 62, in <module> emp = Employee('Bobby Hadz', 100) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Class() takes no arguments TypeError: Object() takes no arguments
Here is an example of how the error occurs.
class Employee(): def get_salary(self): return self.salary # ⛔️ TypeError: Employee() takes no arguments emp = Employee('Bobby Hadz', 100) print(emp)
We haven't defined an __init__() method but are passing arguments to the class.
When a class is instantiated, its __init__()
method gets called with the
supplied arguments.
To solve the error, make sure to define an __init__()
method or correct your
spelling if your class already has one.
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_salary(self): return self.salary emp = Employee('Bobby Hadz', 100) print(emp.name) # 👉️ Bobby Hadz print(emp.get_salary()) # 👉️ 100
__init__
(two underscores on each side).def __init__(self, name, salary): # ✅ Correct def _init_(self, name, salary): # ⛔️ Incorrect def __init(self, name, salary)__: # ⛔️ Incorrect
If you mistype the __init__
method in the class's definition, it won't get
invoked when the class is instantiated.
When a class defines the __init__()
method, the method is invoked when an
instance is created.
The following line calls the __init__()
method of the class.
emp = Employee('Bobby Hadz', 100)
If you pass arguments when instantiating a class, the arguments are passed on to
the __init__()
method.
The __init__()
method isn't supposed to return anything.
Note that the first argument the __init__()
method takes is
self.
def __init__(self, name, salary): self.name = name self.salary = salary
You could name this argument anything because the name self
has no special
meaning in Python.
self
represents an instance of the class, so when we assign a variable as
self.my_var = 'some value'
, we are declaring an instance variable - a variable
unique to each instance.
When defining the __init__
method, make sure you have indented the code block
consistently.
class Employee(): def __init__(self, name, salary): # ⛔️ Forgot to indent the method self.name = name self.salary = salary
If the method isn't indented correctly, you will either get an error or the method won't run when you instantiate the class.
Note that unlike other methods, the __init__
method isn't supposed to return
anything.
The method is used to assign properties to the newly created class instance.
If you use a return statement in your
__init__
method, you'd get an error:
TypeError: __init__() should return None, not 'X'.
You can learn more about the related topics by checking out the following tutorials: