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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

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

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

typeerror not supported between instances of method and int

Here is an example of how the error occurs.

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

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

main.py
class Employee(): def get_salary(self): return 100 emp1 = Employee() my_int = 10 print(emp1.get_salary() > my_int) # 👉️ True

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

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

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

main.py
class Employee(): def get_salary(self): return 100 emp1 = Employee() print(type(emp1.get_salary)) # 👉️ <class 'method'> print(callable(emp1.get_salary)) # 👉️ 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.