[PyKDE] SIP support for __radd__ and friends

Gerard Vermeulen gerard.vermeulen at grenoble.cnrs.fr
Sun Aug 7 18:22:07 BST 2005


On Sun, 7 Aug 2005 16:48:01 +0100
Phil Thompson <phil at riverbankcomputing.co.uk> wrote:

> On Friday 29 July 2005 6:45 am, Gerard Vermeulen wrote:
> > I like to see SIP support (in a class Foo) for slots like:
> > __radd__
> > __rsub__
> > __rmul__
> > __rdiv__
> > __rtruediv__
> > __rfloordiv__
> > __rmod__
> > __rdivmod__
> > __rpow__
> > __rlshift__
> > __rrshift__
> > __rand__
> > __rxor__
> > __ror__
> >
> > so that one can use:
> >
> > any_python_type binary_operator Foo_type
> 
> There was a reason why this wasn't implemented - but I can't remember what it 
> was so I'll look at it again.
> 
> If I do add support I'll probably try and do it transparently, ie. if you 
> define __add__ SIP will automatically generate a corresponding __radd__ so 
> all you'll need to do is to re-build.
> 

A few remarks:

- this feature request goes in a different direction than the last item on
  your SIP TODO list (I do not really understand what you mean).
  I am a little bit worried about the global operators,
  because of possible interference with Numerical Python operators:
>>> from Numeric import *
>>> listA = [0, 1]; listB = [2, 3]
>>> arrayA = array(listA); arrayB = array(listB)
>>> arrayA + arrayB
array([2, 4])
>>> listA + arrayB
array([2, 4])

- let me point out that it is easy to wrap 'friend' operators, so I do not
  really see the need for global operators.  I have working SIP code like:

class SbVec2s
{
    ...
    // friend SbVec2s operator*(const SbVec2s&, int);
    SbVec2s __mul__(int /Constrained/);
    // friend SbVec2s operator*(const SbVec2s&, double);
    SbVec2s __mul__(double);
    // FIXME: friend SbVec2s operator*(int, const SbVec2s&);
    // FIXME: friend SbVec2s operator*(double, const SbVec2s&);
    // friend SbVec2s operator/(const SbVec2s&, int);
    // friend SbVec2s operator/(const SbVec2s&, double);
    SbVec2s __div__(double);
    // friend SbVec2s operator+(const SbVec2s& v1, const SbVec2s&);
    SbVec2s __add__(const SbVec2s&);
    // friend SbVec2s operator-(const SbVec2s&, const SbVec2s&);
    SbVec2s __sub__(const SbVec2s&);
    // friend int operator==(const SbVec2s&, const SbVec2s&);
    bool __eq__(const SbVec2s&);
    // friend int operator!=(const SbVec2s&, const SbVec2s&);
    bool __ne__(const SbVec2s&);

}; // class SbVec2s

- sometimes operators do not commute, for instance:
  matrix * vector != vector * matrix
  so there is the risk that the generated code for __rmul__ might be
  wrong.  Also in the SbVec2s example above there is no definition for
  friend SbVec2s operator/(double, const SbVec2s&);
  so one does not always want the __rdiv__. 

Gerard




More information about the PyQt mailing list