< not supported between instances of 'NoneType' and 'int'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

< not supported between instances of 'NoneType' and 'int' #

The Python "TypeError: '<' not supported between instances of 'NoneType' and 'int'" occurs when we use a comparison operator between a None value and an int. To solve the error, figure out where the None value comes from and correct the assignment.

typeerror not supported between instances of nonetype and int

Here is an example of how the error occurs.

main.py
my_int_1 = None my_int_2 = 10 # ⛔️ TypeError: '<' not supported between instances of 'NoneType' and 'int' if my_int_1 < my_int_2: print('success')

We used a comparison operator between values of incompatible types (None and int) which caused the error.

To solve the error, you have to figure out where the None value comes from and correct the assignment or conditionally check if the variable doesn't store None.

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.

Functions that don't explicitly return a value return None.

main.py
# 👇️ this function returns None def get_int(): print(20) my_int_2 = 10 # ⛔️ TypeError: '>' not supported between instances of 'NoneType' and 'int' if get_int() > my_int_2: print('success')

You can use a return statement to return a value from a function.

main.py
def get_int(): return 20 my_int_2 = 10 if get_int() > my_int_2: # ✅ this runs print('success')

Use an if statement if you need to check whether a variable doesn't store a None value before using a comparison operator.

main.py
my_int_1 = None my_int_2 = 10 if my_int_1 is not None: print(my_int_1 < my_int_2) else: # 👉️ this runs print('Variable stores a None value')

Alternatively, you can set the variable to 0 if it stores None.

main.py
my_int_1 = None my_int_2 = 10 if my_int_1 is None: my_int_1 = 0 print(my_int_1 < my_int_2) # 👉️ True

If the my_int_1 variable stores a None value, we set it to 0 before using a comparison operator.

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
def get_int(a): if a > 5: return a my_int_1 = get_int(4) print(my_int_1) # 👉️ None my_int_2 = 10 # ⛔️ TypeError: '>' not supported between instances of 'NoneType' and 'int' print(my_int_1 > my_int_2)

The if statement in the get_int function is only run if the passed in number is greater than 5.

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

To solve the error, you either have to check if the function didn't return None, or return a default value if the condition is not met.

main.py
def get_int(a): if a > 5: return a return 0 my_int_1 = get_int(4) print(my_int_1) # 👉️ 0 my_int_2 = 10 print(my_int_1 > my_int_2) # 👉️ False

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

Conclusion #

The Python "TypeError: '<' not supported between instances of 'NoneType' and 'int'" occurs when we use a comparison operator between a None value and an int. To solve the error, figure out where the None value comes from and correct the assignment.

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.