## swig-user

 [Swig-user] use of %extend From: Michel Dupront - 2010-04-15 11:14:20 Attachments: Message as HTML Hello, With the following example, given in the documentation: struct Vector { double x,y,z; }; %extend Vector { Vector __add__(Vector *other) { Vector v; v.x = \$self->x + other->x; v.y = \$self->y + other->y; v.z = \$self->z + other->z; return v; } }; one can do in python: >>> import example >>> v = example.Vector(2,3,4) >>> w = example.Vector(10,11,12) >>> print v+w Vector(12,14,16) >>> There is something more that I would like to do but I don't know how. I would like to be able to do: >>>> v1 = v + 2 >>>> v2 = 2 + v where v is a vector and 2 a number that would be added to each component of the vector. I would really appreciate any help on that. Many thanks in advance. _________________________________________________________________ Hotmail et MSN dans la poche? HOTMAIL et MSN sont dispo gratuitement sur votre téléphone! http://www.messengersurvotremobile.com/?d=Hotmail
 Re: [Swig-user] use of %extend From: - 2010-04-15 12:07:48 > > Hello, > > > > With the following example, given in the documentation: > > > struct Vector { > double x,y,z; > }; > %extend Vector { > Vector __add__(Vector *other) { > Vector v; > v.x = \$self->x + other->x; > v.y = \$self->y + other->y; > v.z = \$self->z + other->z; > return v; > } > }; > > > one can do in python: > > >>> import example > >>> v = example.Vector(2,3,4) > >>> w = example.Vector(10,11,12) > >>> print v+w > Vector(12,14,16) > >>> > > > There is something more that I would like to do but I don't > know how. I would like to be able to do: > > >>>> v1 = v + 2 > >>>> v2 = 2 + v > > where v is a vector and 2 a number that would be added to each component > of the vector. > I would really appreciate any help on that. > Many thanks in advance. > > > Hi Michel, how about something like %extend Vector { Vector __add__(Vector *other) { Vector v; v.x = \$self->x + other->x; v.y = \$self->y + other->y; v.z = \$self->z + other->z; return v; } Vector __add__(double scalar) { Vector v; v.x = \$self->x + scalar; v.y = \$self->y + scalar; v.z = \$self->z + scalar; } Vector _radd__(double scalar) { Vector v; v.x = \$self->x + scalar; v.y = \$self->y + scalar; v.z = \$self->z + scalar; return v; } } This should work for __sub__, __rsub__ etc. accordingly. I haven’t tested this code. Philipp -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01