Vector classes

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Apr 22 18:34:17 EDT 2007


En Sun, 22 Apr 2007 09:33:53 -0300, Mizipzor <mizipzor at gmail.com> escribió:

> During my coding Ive found two vector classes on the internet. Ive
> modified them both a little but the do both have advantages and
> disadvantages.
>
> vector1: http://rafb.net/p/4FVdh699.html
> vector2: http://rafb.net/p/0KShGu30.html
>
> With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
> vec.vals[0] and vec.vals[1], which makes my eyes bleed.

[2] modifies the 2nd argument to __add__ and __sub__, not a good thing...
The only advantage I can see over [1], is that it handles dimensions  
higher than 2.

> But with 2, I can create a vector by supplying a list of numbers as
> arguments.

Same with the first one:

coords = (1.2, 3.4)
v1 = vector(*coords)

There is even the "list" static method (not a good name!):

coords = (1.2, 3.4)
v1 = vector.list(coords)

Anyway I'd remove the __ne__ and __lt__ methods (if you don't require it)  
and replace __hash__ with hash((self.x,self.y))

> Is there any way to combine the two? The ultimate would be if I
> somehow could take vector2 and hook the member self.vals[0] to self.x
> or something.

You should try the other suggested classes, but if I had to choose among  
these two, I'd use the first.

-- 
Gabriel Genellina



More information about the Python-list mailing list