Why does my function print None in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Feb 18, 2023
3 min

banner

# Why does my function print None in Python [Solved]

Functions often print None when we pass the result of calling a function that doesn't return anything to the print() function.

All functions that don't explicitly return a value, return None in Python.

main.py
def example(): print('bobbyhadz.com') print(example()) # ๐Ÿ‘‡๏ธ Output # bobbyhadz.com # None

all functions that dont return value return none

Notice that we called the print() function twice.

When we call the example() function, the print() function gets called with hello world, and then we print the result of calling the example() function.

# Functions that don't return anything return None

The example function doesn't return anything, so it ends up implicitly returning None, so None gets printed.

If we were to return a value from the function, the value would get printed.

main.py
def example(): print('bobbyhadz.com') return 'abc' print(example()) # ๐Ÿ‘‡๏ธ Output # bobbyhadz.com # abc

We used the return statement to return a value from the function.

Now when the example() function is called, we print the strings bobbyhadz.com and abc, instead of None.

Note that all functions that don't explicitly return a value, end up implicitly returning None.

If you don't want to return anything from the function, remove the print() call when calling the example() function.

main.py
def example(): print('hello world') example() # ๐Ÿ‘‡๏ธ Output # hello world

There is no point in printing the result of calling a function that doesn't return anything because we're always going to print None.

# The most common sources of None values in Python

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.
Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

# Some built-in methods return None

Some built-in functions, such as sort(), append(), extend() return None because they mutate the object in place.

main.py
a_list = ['z', 'a', 'b', 'c'] print(a_list.sort()) # ๐Ÿ‘‰๏ธ None print(a_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'z']

some built in methods return none

Notice that the call to the list.sort() method printed None.

The list.sort() method sorts a list in place and returns None.

Make sure to not store the result of calling methods that return None in variables.

main.py
a_list = ['z', 'a', 'b', 'c'] # โ›”๏ธ don't do this result = a_list.sort() print(result) # ๐Ÿ‘‰๏ธ None print(a_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'z']

There is a convention in Python for methods that mutate an object in place to return None.

# A function that returns a value only if a condition is met

Another common source of None values is having a function that returns a value only if a condition is met.

main.py
def get_list(a): if len(a) > 3: return a # ๐Ÿ‘‡๏ธ None my_list = get_list(['a', 'b']) print(my_list) # ๐Ÿ‘‰๏ธ None

functions returning value only if condition is met

The if statement in the get_list function is only run if the passed in argument has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To get around this, we could return a default value if the condition is not met, e.g. an empty string, an empty list, 0, or any other value that suits your use case.

main.py
def get_list(a): if len(a) > 3: return a return [] # ๐Ÿ‘ˆ๏ธ return an empty list if condition not met # ๐Ÿ‘‡๏ธ [] my_list = get_list(['a', 'b']) print(my_list) # ๐Ÿ‘‰๏ธ []

Now the function is guaranteed to return a value regardless if the condition is met.

# 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.

Copyright ยฉ 2024 Borislav Hadzhiev