How To Prevent A Function From Returning None In Python

Sometimes, The functions may return None in Python because they do not return any values after performing. So how can you prevent a function from returning None in Python? This tutorial will explain it right below.

In which cases do functions return None in Python?

Typically, the functions will return None in these cases:

  • The function does not return any values.
  • The function has the ‘return’ method, but it is not performed after calling.
  • The function returns None with your cases.

Look at the example below to learn more about it.

import math

def my_func(a, b):
    a = math.pow(a, b)

def greeting(option):
    if option == 1:
        return 'Hello, I am PythonDev. Have a nice day!'

def none_func():
    return None

# All of these manipulations will return None.
print(my_func(10, 2))
print(greeting(0))
print(none_func())

Output

None
None
None

Prevent A Function From Returning None In Python

From those reasons that make the functions return None. You can prevent them by doing this:

  • Having the function that returns any values.
  • Prevent the cases that do not return anything.
  • Prevent the functions that return None.
  • If the function does not return anything, you can call this function without printing it.

Having the function that returns any values

A simple way to prevent a function from returning None is to have a function that returns any values. So, when you print it, the result will be the values that you return.

Look at the example below.

import math

def my_func(a, b):
    a = math.pow(a, b)
    return int(a)

# Print the result of this function.
print(my_func(10, 2))

Output

100

Prevent the cases that do not return anything

Besides, you may encounter cases in that you do not return anything. To prevent it, make sure you assign parameters properly.

Look at the example below.

def greeting(option):
    if option == 1:
        return 'Hello, I am PythonDev. Have a nice day!'

# Print the result of this function.
print(greeting(1))

Output

Hello, I am PythonDev. Have a nice day!

Prevent the functions that return None

Sometimes, you create a function that returns None. To prevent it, you should return other values except None.

Look at the example below.

def none_func():
    # return None: prevent it.
    return False

# Print the result of this function.
print(none_func())

Output

False

Call the function without printing it

Moreover, a function may be used to do something or manipulate, change the specified variable. In this case, you should call this function without printing it.

Look at the example below.

def hello():
    print('Hello!')

hello()

Output

Hello!

Summary

We have shown you how to prevent a function from returning None in Python in 4 ways. Typically, this problem occurs when the function does not return anything or returns None. So, you should be careful to prevent them when creating a function. Have an enjoyable learning experience. Thanks!

Leave a Reply

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