Q:

Complex operations with Python (pygame.math.Vector2)

I'm learning Python and came across a complex expression that derives from pygame.Vector2:
import pygame
x = pygame.math.Vector2 (1,2)
b = x * 5 - (1, 2)
print (x)
print (b)

Result:
[1,2]
[4,8]

In the above case, the same x * 5 operation is performed both for the 1 and 2 values of Vector2, resulting in (5, 10) respectively; and then both results are subtracted from the tuple (1, 2), resulting in [4,8]
However if I do assign a simple tuple to x: x = (1, 2), instead of Vector2, I get the error:

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

My question is: At what times in Python I can perform these complex operations?

A:

Can do something like (see comments too):
x = (1,2) # create a `tuple`
b = map(lambda x: x * 5,x) # do a `map` object for multiplying 5 to all of them
print(x) # print the `tuple`
t=iter((1,2)) # do an generator object using `iter`, so able to use `next` to access every next element
print(tuple(map(lambda x: x-next(t),b))) # do the `next` and another `map`, to subtract as you wanted

Best thing is still to create a class:
from __future__ import division
class MyClass:
   def __init__(self,t):
      self.t=t
   def __mul__(self,other):
      return MyClass(tuple(map(lambda x: x*other,self.t)))
   def __truediv__(self,other):
      return MyClass(tuple(map(lambda x: x/other,self.t)))
   def __sub__(self,other):
      gen=iter(other)
      return MyClass(tuple(map(lambda x: x-next(gen),self.t)))
   def __add__(self,other):
      gen=iter(other)
      return MyClass(tuple(map(lambda x: x+next(gen),self.t)))
   def __repr__(self):
      return str(tuple(self.t))

Then now can do anything:
x = MyClass((1,2))
b = x*5
print(b)
print(b-(1,2))

Output:
(5, 10)
(4, 8)

Also can do addition:
x = MyClass((1,2))
b = x*5
print(b)
print(b-(1,2)+(3,4))

Output:
(5, 10)
(7, 12)

Also division:
x = MyClass((1,2))
b = x*5
print(b)
print((b-(1,2)+(3,4))/2)

Output:
(5, 10)
(3.5, 6.0)

