Warning: session_start(): open(/tmp/sess_295c8a72c9066ef032d75696aa6c9d00, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Solutions to fix TypeError: __init__() should return None, not 'int' in Python - LearnShareIT

Solutions to fix TypeError: __init__() should return None, not ‘int’ in Python

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

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:

Leave a Reply

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