TypeError: __init__() should return None, not 'X' (Python)

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: __init__() should return None, not 'X' (Python) #

The Python "TypeError: __init__() should return None, not 'X'" occurs when we return a value from the __init__() method in a class. To solve the error, remove the return statement from the method because the __init__ method in a class should always implicitly return None.

typeerror init should return none not

Here is an example of how the error occurs.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary # 👇️ remove this return statement return {'name': name, 'salary': salary} # ⛔️ TypeError: __init__() should return None, not 'dict' emp1 = Employee('Bob', 75)

The error is caused because we returned a value from the __init__() method.

The __init__ method should always return None (implicitly), so remove the return statement to solve the error.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary emp1 = Employee('Bob', 75) print(emp1.name) # 👉️ 'Bob' print(emp1.salary) # 👉️ 75

If we return a value other than None from a class's __init__() method, a TypeError is raised.

Technically, you could return None from the __init__() method but there is no good reason to do that because all functions and methods that don't explicitly return a value, return None.

When a class defines the __init__() method, the method is invoked when an instance is created.

The __init__() method isn't supposed to return anything.

main.py
class Employee(): country = 'Belgium' def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name emp1 = Employee('Bob', 75) print(emp1.get_name()) # 👉️ 'Bob' print(emp1.salary) # 👉️ 75
Make sure your indentation is correct and you haven't misspelled __init__ (two underscores on each side).

If you pass arguments when instantiating a class, the arguments are passed on to the __init__() method.

The name and salary instance variables in the example above are unique to each instance, as opposed to the country class variable which is shared by all instances.

Note that the first argument the __init__() method takes is self.

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.

Conclusion #

The Python "TypeError: __init__() should return None, not 'X'" occurs when we return a value from the __init__() method in a class. To solve the error, remove the return statement from the method because the __init__ method in a class should always implicitly return None.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.