Home > other >  Python: Operandtype-Error in a method, but not in main, with the same code
Python: Operandtype-Error in a method, but not in main, with the same code

Time:07-18

im new in programming with python and tried to make a own vector class. This class has a variable values, which is a list of integer values. The constructor looks like this:

def __init__(self, *args: int):
    if len(args) == 0: self.values = [0, 0]
    else: self.values = [value for value in args]

I wanted to implement a method, called "vector_sum" which is a static method and shall return a Vector with the values of the sum of the corresponding elements. For example (just the calculation): sum((1,2),(3,4),(5,6))=(9,12)

My method looks like this:

def vector_sum(vectors: List[Vector]) -> Vector:
        vals: List[int] = []
        for i in range(len(vectors)):
            for j in range(len(vectors[i].values)):
                if i == 0:
                    vals.append(vectors[i].values[j])
                else:
                    vals[j]  = vectors[i].values[j]
        z: Vector = Vector(*vals)
        return z

When i want to print the result of the method there is this error:

Traceback (most recent call last):
  File "...", line 61, in <module>
    print(sum(vectors).values)
TypeError: unsupported operand type(s) for  : 'int' and 'Vector'

I am very confused, because if i try to run this exact same code via main, it works just fine:

if __name__ == '__main__':
    v: Vector = Vector(1, 2)
    w: Vector = Vector(3, 4)
    x: Vector = Vector(5, 6)
    vectors: List[Vector] = [v, w, x]
    vals: List[int] = []
    for i in range(len(vectors)):
        for j in range(len(vectors[i].values)):
            if i == 0:
                vals.append(vectors[i].values[j])
            else:
                vals[j]  = vectors[i].values[j]
    z: Vector = Vector(*vals)
    print(z.values)

This prints out following result:

[9, 12]

Process finished with exit code 0

I can not explain why this happens. It's so confusing, that it runs in main but not in the method, although it is the exact same code.

PS: Of course i use the list "vectors" for both scenarios.

It would be nice if you could help me. Greetings Moe

CodePudding user response:

You need to call vector_sum, the function you created, rather than sum:

print(vector_sum(vectors).values)

CodePudding user response:

sum takes a second, optional argument to specify the initial value. If you don't supply it, it defaults to 0 rather than assuming the iterable argument is empty. That's why sum([]) == 0 and sum([1]) == 1 0 == 1. Basically, sum behaves like

def sum(itr, init=0):
    rv = init
    for x in itr:
        rv  = x
    return rv

The initial value should be the identify for the actual addition operator. 0 works fine for numbers, since x 0 == 0 x == x for numeric types. It does not work for your vectors, because you haven't specified what v 0 means. You need to do that, or provide an identity vector as the second argument to sum.

(All this assumes you have defined Vector.__add__ to allow sum to add vectors at all.)

  • Related