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