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

Re: [pygame] vector type: mutable or immutable





René Dudfield schrieb:
hrmmm.  Good question.

Thinking in terms of how much I use lists VS tuples...
   I'd use mutable lists.... for 96% of cases...  but occasionally
would like to use an immutable tuple.

Another argument is that Rect, Surface, and Color are mutable - so it
would be somewhat consistent with the other types.
Although I'm rather new on this list (yet), I'd like to contribute to this important thread:

Imho vectors should preferably behave like numbers (as we need them for calculations) instead of like Rects, Surface etc. In other words, it should be consistent more with number types than with 'other types'. A code line like

x = x + v * dt

with x, v vectors is more clear and useful if I don't have to think which other names there are for the object x (and thus will be changed together with x). I suspect thie use of a mutable vector type
would make the use of it more error-prone.

So in the first place we should have immutable vectors. Maybe there are also use cases which would benefit from a mutable vector type, so to have both would be beneficial. If so a careful consideration of the names of the datatypes would be necessary. VectorConst for something which is not a constant doesn't seem appropriate to me. (Or would you agree to name the int type intconst?)

Moreover I think, that implementation details (memory management etc) should not override
problem domain arguments.

Regards,
Gregor




Note that Sprite, and Surface are mutable, but can be used as
dictionary keys.  They don't use their values as the keys.

However Rect can not be used for keys... so that's an inconsistency -
but maybe a good one.  Since generally you would want to use the
values of the Rect as the keys - so it forces a conversion to tuple.

I wonder how much effort it would be to make immutable and mutable?  I
think maybe too much.

overall... +1 for mutable.




cu,


On Sat, May 2, 2009 at 12:07 AM, Lorenz Quack <don@xxxxxxxxxxxxxxxxx> wrote:
Hi

as a follow up to the "API draft for vector type" I would like to discuss
the merits of having a mutable or immutable vector type.


Arguments for an immutable vector type:

Brian Fisher pointed out two advantages of immutable vector types:
1) they prevent bugs in programs like the following (adopted from Brian):
class Angel(object)
    def __init__(self, offset):
        self.offset = offset

t = Angel()
halo_pos = t.offset
halo_pos.y -= 5  # BUG: this changes the offset in t
2) if vectors are immutable you can use them as keys in dicts



Arguments for a mutable vector type:

1) operations such as rotate() and scale_to_length() are more elegant when
operation in-place. for immutable types you would have to do "v =
v.rotate()" or use a module level rotate function "v =
pygame.math.rotate_vector(v)" or something similar.

2) a priori I would expect mutable vectors to perform better than immutable
ones because you don't have to create new objects for every operation.



So are there anymore arguments?
And where do people stand on this issue?


yours

//Lorenz