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

Viewed 32117

I hoping someone can help with this.

I have created a class with a function in it that counts the total cars in 4 lists of cars.

On another script I am creating the interface and want to say if the answer to 'totalCars' is bigger than zero then proceed to offer a type of car.

However when I do this I get this error: TypeError: '>' not supported between instances of 'method' and 'int'. Here is the code:

def totalCars(self):
    p = len(self.getPetrolCars())
    e = len(self.getElectricCars())
    d = len(self.getDieselCars())
    h = len(self.getHybridCars())
    totalCars = int(p) + int(e) + int(d) + int(h)
    return totalCars 

And on the interface script have:

while self.totalCars > 0:

To get around this I tried to use a boolean, like this:

def totalCars(self):
    p = len(self.getPetrolCars())
    e = len(self.getElectricCars())
    d = len(self.getDieselCars())
    h = len(self.getHybridCars())
    totalCars = int(p) + int(e) + int(d) + int(h)
    if totalCars > 0:
        return True 

And on the app script I have:

 while self.totalCars is True

But this totally crashed the program and won't run at all.

Any guidance welcome here. Many thanks.

4 Answers

That's because self.totalCars is a method and you need to call it to get it's return value by adding a couple parenthesis at the end, like so:

while self.totalCars() > 0:
     #Insert the rest here

Otherwise, like the message says, you're comparing a method with a number, and that's not gonna work.

No need to add a boolean, but if you insisted on using one, you could do something like:

while self.totalCars():    #Will run if self.totalCars() RETURNS True

Again, this didn't really work in your original code because you forgot the parenthesis.

Hope this helps.

For your last question, you just need this:

while self.totalCars():

Call the method, but don't check against Trueif it returns a Boolean.

Also

def totalCars(self):

should end with:

return False

You should also point the exact location of the error next time.

Without calling method, no data is return so it can't be compared to a number.

you just add "()"

while self.totalCars > 0:
=> while self.totalCars() > 0:

Same when you are using count,

I had this issue when I were using count

Model1.objects.filter(user=self.user).count > 1

Adding parentheses after count will make it work

Model1.objects.filter(user=self.user).count() > 1
Related