[PyKDE] How to do operators with SIP

Ewald R. de Wit erdewit at zonnet.nl
Fri Nov 16 23:42:04 GMT 2001


Phil Thompson (phil at river-bank.demon.co.uk) wrote:

> Implementing the '+' operator for QPoint would look something like...
> 
> PyNumberMethod __add__
[snip]

Thanks Phil. I have tried something similar for SIP 2.5 but noticed
that the operators do not get inherited by sub classes. So what
I did was to make classes like Add, Substract etc in C++; they act
as prefix operators. After that I added something like this to the
SIP code:

-----------------------------------------------------------------
%PythonCode
Class Ops:
	def __add__( self, r ):
		return Add( self, r )
	def __radd__( self, r ):
		return Add( r, self )
	... etc ...
%End
-----------------------------------------------------------------

and for each operator

-----------------------------------------------------------------
class Add : Indicator
{
%HeaderCode
#include "../../indicator.h"
%End
public:
	Add( Indicator, Indicator );
	Add( Indicator, double );
	Add( double, Indicator );
};
%PythonCode
class Add( Add, Ops ): pass
%End
-----------------------------------------------------------------

The class called 'Ops' can easily be inherited by subclasses, by
first making the subclass with SIP and after that redefining the
subclass by letting it inherit both from itself and from Ops.

I works great for me and I hope this is of use to others.

-- 
  --  Ewald






More information about the PyQt mailing list