Extending: overloading operators (e.g. for a vector class)

Fernando Pérez fperez528 at yahoo.com
Tue May 21 23:22:21 EDT 2002


Chris Liechti wrote:

> say i have the follwing class (details ommited):
> 
> class Vector2D:
>     def __init__(self, x=0, y=0):
>         self.x, self.y = x, y
>     ...
>     def __mul__(self, other):
>         if type(other) in (int, float):
>             return Vector2D( self.x*other, self.y*other )
>         else:
>             raise NotImplementedError
> 
> now i want to program this as a C extension type (for practise, i know that
> there are existing extensions to represent a vector).

Why don't you poke a bit through the Numeric sources? I'm sure they are quite 
a valuable source of the gory details on how to do this, since a numeric 
array() has overloaded all arithmetic operators in various fancy ways.

Good luck,
f



More information about the Python-list mailing list