adding vectors

Terry Hancock hancock at
Tue Dec 20 08:02:01 CET 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
Anansi Spaceworks

More information about the Python-list mailing list