'>' not supported between instances of 'function' and 'int'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

'>' not supported between instances of 'function' and 'int' #

The Python "TypeError: '>' not supported between instances of 'function' and 'int'" occurs when we use a comparison operator between a function and an integer. To solve the error, make sure to call the function with parenthesis, e.g. my_func().

typeerror not supported between instances of function and int

Here is an example of how the error occurs.

main.py
def get_num(): return 50 my_int = 10 # ⛔️ TypeError: '>' not supported between instances of 'function' and 'int' print(get_num > my_int) # 👈️ forgot to call method
We forgot to call the function with parenthesis, e.g. emp.get_salary(), so our code actually tries to compare a function and an integer.

To solve the error, make sure to call the function.

main.py
def get_num(): return 50 my_int = 10 print(get_num() > my_int) # 👉️ True

We used parenthesis to invoke the function, so now we compare the return value of the function with an integer.

If your function takes arguments, make sure to pass them when calling it, e.g. get_num(10, 20).

If you aren't sure what type of object a variable stores, use the type() class.

main.py
def get_num(): return 50 print(type(get_num)) # 👉️ <class 'function'> print(callable(get_num)) # 👉️ True my_int = 10 print(type(my_int)) # 👉️ <class 'int'> print(isinstance(my_int, int)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

The callable function takes an object as an argument and returns True if the object appears callable, otherwise False is returned.

If the callable() function returns True, it is still possible that calling the object fails, however if it returns False, calling the object will never succeed.

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.