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

Chris Liechti cliechti at gmx.net
Wed May 22 17:37:59 EDT 2002


Fernando Pérez <fperez528 at yahoo.com> wrote in
news:acf32s$nor$2 at peabody.colorado.edu: 

> 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

i already have downloaded it but it not so easy to read that code... i.e. 
the operators don't seem to be directly in the array object but separately 
passed through a dict. folowing that traces is not so simple.

i just hoped to find a more stright forward and simpler example - but it's 
not _that_ important.

thanks anyway

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list