Qubix
Qubix

Reputation: 4353

Unsupported operand type using sum function

Here is a snippet containing a code that reproduces my error. Why does the sum function pass a class instance instead of a number to the subtract method?

class my_class():
    def __init__(self):
        pass

    def __subtract (x,y, n=1):
        return (x - y)**n

    def num_generator(self, num):
        example_list = []
        for idx in range(num):
            example_list += [idx]

        expression = {y: sum(self.__subtract(x, y) for x in example_list) for y in range(min(example_list), max(example_list) + 1)}
        no_vals = max(expression.items(), key=operator.itemgetter(1))[0]
        return no_vals

chosen_no = 20
class_instance = my_class()
class_instance.num_generator(chosen_no)

ERROR:

TypeError: unsupported operand type(s) for -: 'my_class' and 'int'

I have been staring at this for 20 minutes now and to me it makes no sense.

Upvotes: 1

Views: 95

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140276

in:

def __subtract (x,y, n=1):
    return (x - y)**n

since __subtract is an instance method, x represents the current object as first argument (no, self isn't a keyword).

The default argument makes it undetectable which explains that python doesn't complain about the number of arguments. When you call:

self.__subtract(x, y)

in the call you get all arguments shifted, and y occupying the n default argument slot:

def __subtract (yourobject => x, x => y, y => n):

Your method doesn't need the object state so just add the @staticmethod decorator

@staticmethod
def __subtract (x,y, n=1):
    return (x - y)**n

as an aside, replace the cumbersome & underperformant code below:

    example_list = []
    for idx in range(num):
        example_list += [idx]

by

example_list = list(range(num))

Upvotes: 3

Related Questions