If you’re new to OOP, you’ll probably encounter the TypeError: __init__() should return None, not ‘int’ in Python. Fortunately, this error is not serious, and it is also quite simple to fix. In this post, we will discuss the leading cause of this error and how to use OOP knowledge to avoid it in the future.
What is __init__()
?
__init__()
is a special method in Python class. This method is called when an instance of the class is created. It can be used to initialize the attributes of the class. __init__()
is usually used to set the values of instance variables.
When does the TypeError: __init__() should return None, not ‘int’ in Python occur?
In Python, the constructor __init__()
is not allowed to return any value. As a result, when we declare this method, we are not permitted to include a return statement. If we return something different than None, the program will throw an error. To understand the error, look at the code below:
class Website: # Try to use return in the __init__() def __init__(self, name, url): self.name = name self.url = url return 1 # Create an object of the Website class website = Website('LearnShareIT', 'https://learnshareit.com/')
Output:
TypeError: __init__() should return None, not 'int'
How to avoid this error?
Remove return statement
The simplest approach to avoid this error is removing any return statements from the __init__()
method.
In our previous example, the __init__()
method contains a return statement; let’s remove it and see what happens.
class Website: # Create a __init__() method without return statement def __init__(self, name, url): self.name = name self.url = url # Create an object of the Website class website = Website('LearnShareIT', 'https://learnshareit.com/') # Do something with the website print(website.name)
Output:
LearnShareIT
Set the return statement to None
Because the __init__()
method can only return None
. So, we will set it to None. Let’s see what happens to our Website class if we try that.
class Website: # Create a __init__() method with return None def __init__(self, name, url): self.name = name self.url = url # Set the return statement to None return None # Create an object of the Website class website = Website('LearnShareIT', 'https://learnshareit.com/') # Do something with the website print(website.name)
Output:
LearnShareIT
Use getter
In OOP programming, getters are commonly used to get information about an object, such as its name. So, you should use the getter instead of the return statement in the __init__()
method to get any value from the class’s attributes.
class Website: # Create a __init__() method without return statement def __init__(self, name, url, age): self.name = name self.url = url self.age = age # Create the getter def getAge(self): return self.age # Create an object of the Website class website = Website('LearnShareIT', 'https://learnshareit.com/', 1) # Use getter to get the age of website print('Age:', website.getAge())
Output:
Age: 1
From now on, use the getter described above to ensure the encapsulation of the class in Python.
Summary
The TypeError: __init__() should return None, not ‘int’ in Python is a common error. When you see this error, it is important to understand what causes it before following the methods outlined in this article to solve it. You should also follow some OOP programming principles to avoid this error and keep your Python code running smoothly.
Maybe you are interested:
- TypeError: Object of type Timestamp is not JSON serializable
- TypeError: object of type ‘generator’ has no len() in Python
- TypeError: ‘range’ object does not support item assignment

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java