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

Re: [pygame] vector type: mutable or immutable



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.

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
>