How To Resolve TypeError: _ init _() Should Return None, Not ‘#’ In Python

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

To fix TypeError: __init__() should return None, not ‘#’ in Python, in simple cases, you can remove the return statement from the __init__ function or use the print function. Read the following article to understand more.

What causes the TypeError: __init__() should return None, not ‘#’ in Python?

The __init__ function is the constructor function of the inner class. All classes in Python are initialized with the __init__ function. The __init__ function is used to assign values ​​to an object’s attribute or perform some operations when the object is created.

Syntax:

def __init__(self, [parameter1, parameter2,...]):

Parameters:

  • def: key declares the function.
  • __init__: the name of the constructor.
  • self: the first parameter of the function. Used to access class variables.
  • parameter1, parameter2: optional parameters are values ​​to assign to object properties.

The TypeError: __init__() should return None, not ‘#’ happens because the __init__ function is not allowed to return any value. So when you declare the function __init__, you are not allowed to include a return statement because it will return a value other than None. 

Note: ‘#’ is for generalizing the return value.

Example: 

class Information:
    # Constructor with 2 parameters and return
    def __init__(self, Information_name, Information_age):
        self.name = Information_name
        self.age = Information_age
        return True

    def myInfo(self):
        print(self.name,self.age)

result = Information('John','18 yearsold')
result.myInfo()

Output:

Traceback (most recent call last):
 File "prog.py", line 11, in <module>
    result = Information('John','18 yearsold')
TypeError: __init__() should return None, not 'bool'

As you can see in the above error example, I return True in the __init__ function, and the program gives a TypeError: __init__() should return None, not ‘bool’. Here is the workaround.

How to solve this error?

Remove the return statement

Example:

  • The cause I have stated as a workaround is simply that you remove the return statement so that the function returns None and not some other value.
class Information:
    # Constructor with 2 parameters and return
    def __init__(self, Information_name, Information_age):
        self.name = Information_name
        self.age = Information_age

    def myInfo(self):
        print(self.name,self.age)

result = Information('John','18 yearsold')
result.myInfo()

Output:

    John 18 years old

Use the print() function

A function that is too familiar even for those new to programming. If not in complex situations, you can simplify outputting an object to the screen with the print function. As for the __init__ function, you can use it to report errors related to exception errors.

Example:

class Information:
    # Constructor with 2 parameters and return
    def __init__(self, Information_name, Information_age):
        self.name = Information_name
        self.age = Information_age

    def myInfo(self):
        print('John, 18 years old.')
        
result = Information('John','18 yearsold')
result.myInfo()

Output:

John, 18 years old.

Summary

My writing is over. The TypeError: __init__() should return None, not ‘#’ in Python problem should be easy to deal with, right? Hope you got an idea to solve this problem. Good luck!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *