Borislav Hadzhiev
Last updated: Apr 20, 2022
Photo from Unsplash
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()
.
Here is an example of how the error occurs.
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
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.
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.
get_num(10, 20)
.If you aren't sure what type of object a variable stores, use the type()
class.
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.