0

I'm currently working on a class that creates balls and gives them positions. I want to make a add method to the class, that takes the positions of two balls and adds them together. The code I'm working with is:

class Ball:
    def __init__ (self, x, y):
        self.position = [x, y]
    def __add__ (self, other):
        return self.position + other.position
    
ball1 = Ball(3, 4)
print (ball1.position)
ball2 = Ball(5, 8)
print (ball2.position)
ball3 = Ball(4, 4)
print (ball3.position)
ball4 = ball1 + ball3
print (ball4)

The way the code works right now is not as intended. I want ball1 + ball3 positions to add up, but the print I get is like this:

[3, 4]
[5, 8]
[4, 4]
[3, 4, 4, 4]

We have the x and y values of ball1 and ball3 put side by side rather then getting added up.

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Red Panda
  • 1
  • 1
  • 1
    You have to add the individual items in the lists, not just the two lists. – Klaus D. Oct 23 '20 at 03:58
  • The `+` is treated as **concatenation** for lists (sequences). See [Mapping Operators to Functions](https://docs.python.org/3/library/operator.html#mapping-operators-to-functions) – Gino Mempin Oct 23 '20 at 04:02

4 Answers4

1

When you add two lists together it simply appends. Your add would need to look like this:

def __add__ (self, other):
    return [self.position[0]+other.position[0], self.position[1]+other.position[1]]
0

Add the items individually with list comprehension and zip using:

[b1 + b3 for b1, b3 in zip(ball1.position, ball3.position)]

class Ball:
    def __init__ (self, x, y):
        self.position = [x, y]
    def __add__ (self, other):
        return self.position + other.position

ball1 = Ball(3, 4)
print (ball1.position)
ball2 = Ball(5, 8)
print (ball2.position)
ball3 = Ball(4, 4)
print (ball3.position)
ball4 = [b1 + b3 for b1, b3 in zip(ball1.position, ball3.position)]
print (ball4)

[3, 4]
[5, 8]
[4, 4]
[7, 8]

EDIT: You can simplify the list comprehension further to:

ball4 = [sum(b) for b in zip(ball1.position,ball3.position)]
David Erickson
  • 16,433
  • 2
  • 19
  • 35
0

When working with arrays, the "+" operator joins the elements from both operands and returns a new array. So the plus is not doing what you think it is.

You have to develop another function that sums each member of the array and return this new array.

0

That is because you are using sum operator between 2 python lists, which will concatenate the two lists. If you want to sum up the elements element-wise, you will have to do it this way:

return [self.x+other.x, self.y+other.y]

I also suggest you have a look at numpy library, which will give mathematical operators as you intended. In that case, your class can be rewritten as:

import numpy as np

class Ball:
    def __init__ (self, x, y):
        self.position = np.array([x, y])
    def __add__ (self, other):
        return self.position + other.position
    
ball1 = Ball(3, 4)
print (ball1.position)
ball2 = Ball(5, 8)
print (ball2.position)
ball3 = Ball(4, 4)
print (ball3.position)
ball4 = ball1 + ball3
print (ball4)

The results:

>>> ball1 = Ball(3, 4)
>>> print (ball1.position)
[3 4]
>>> ball2 = Ball(5, 8)
>>> print (ball2.position)
[5 8]
>>> ball3 = Ball(4, 4)
>>> print (ball3.position)
[4 4]
>>> ball4 = ball1 + ball3
>>> print (ball4)
[7 8]
Linh Le Vu
  • 436
  • 4
  • 7