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.