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

Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min


# 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

TypeError: __init__() should return None, not 'int'

Here is an example of how the error occurs.

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 shouldn't have a return statement

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

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

the init method should not have return statement

The __init__ method is used to set the supplied values on the instance and shouldn't return a value.

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.

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.

I've also written an article on the purpose of 'return self' from a class method.

Want to learn more about working with classes in Python? Check out these resources: Creating class instances from a Dictionary in Python,Creating class instances from a Dictionary in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.