adding vectors

Terry Hancock hancock at anansispaceworks.com
Tue Dec 20 02:02:01 EST 2005


On Mon, 19 Dec 2005 22:06:31 -0500
Andy Leszczynski <yahoo at nospam.leszczynscy> wrote:
> Short question: why (1,"abc",0.3)+(2,"def",10.2) !=
> (3,"abcdef",10.5)?
> 
> How to elegantly achieve (3,"abcdef",10.5) as a result of
> addition ...

(a,b,c) is a "tuple", not a "vector".

IMHO, the "elegant" thing to do is to define a vector class
and use it. For convenience, allow a tuple initializer:

V = Vector
a = V(1,"abc",0.3) + V(2,"def",10.2)

Of course, the class "Vector" will have to define math
operators appropriately.  Note that "%" has the correct
precedence to sub for cross-product, and sort of looks like
an X if you squint hard enough ;-).

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list