1

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.

Qubix
  • 4,161
  • 7
  • 36
  • 73

1 Answers1

3

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))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I forgot to add self???? Ok, this is really embarrassing. Thanks, works like a charm now. – Qubix Jun 18 '18 at 09:01
  • @Qubix you don't use self in the function though. I'd make it a `staticmethod` as Jean-Francois recommends. More info: https://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python – FHTMitchell Jun 18 '18 at 09:02