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

Greg Ewing look at replyto.address.invalid
Wed May 22 22:56:20 EDT 2002


Chris Liechti wrote:
> 
> it seems to me tp_as_number supports __mul__ for numbers, but only for
> instances of the same type, 

Looking at the interpreter code, it looks like if you set
the NEW_STYLE_NUMBER flag in the type object, the object's
binary operator methods are called directly, regardless of
the other operand type, and without any coercion.

> and there seems so be an other place where a __mul__ can be emulated:
> sq_repeat for sequences. can/must i define both, or none?

It also seems that the sq_repeat slot is never called
from interpreted code any more.

So, it appears that if you:

   1) set the NEW_STYLE_NUMBER flag
   2) put your method in the tp_as_number->nb_multiply slot

you will get the same result as you did with the
Python version.

-- 
Greg Ewing, Computer Science Dept, 
University of Canterbury,	  
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list