[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] immutable vectors in math package



Hello list,

I recently got convinced that immutable types are preferable (massive generalization, I know).
Therefore I went ahead and created a branch making the Vector2 and Vector3 types in the pygame.math package immutable.
If you want to check it out the branch is called immutable_vectors[1].
What changed?
 * Vector2/3 are now immutable
 * you cannot access the components x/y/z directly. either use indexing or the new accessor methods x()/y()/z()
 * the inplace version of methods are gone (e.g., rotate_ip())
 * inplace operators still work but create and return a new object. Observe:
>>> a = b = Vector2(1, 2)
>>> assert id(a) == id(b)
>>> b += Vector(3, 4)
>>> assert id(a) != id(b)
>>> assert a.x() == 1 and a.y() == 2
>>> assert b.x() == 4 and b.y() == 6
 * Vector2.from_polar() and Vector3.from_spherical() are now classmethods

The math package is still marked as experimental so I think that it might be alright to merge this change if desired.

Cheers,
Lorenz


[1] https://bitbucket.org/pygame/pygame/branch/immutable_vectors