Help: Rich comparison in derived class, but not in base (boost.python)

Pearu Peterson pearu at cens.ioc.ee
Wed Mar 21 04:14:29 EST 2001


Hi!

I am using boost.python for wrapping C++ classes and since boost.python 
does not yet support rich-comparison (any ideas, where I should send
such a request? I am not able to subscribe to boost list:( ). 

I tried to do the wrapping in Python. But that seems to not work either: 
I define a new class `expr', derived from a base `ex' (exposed with
boost.python) that does not have rich-comparisons, with __lt__, etc
methods:

class expr(ex):
    def __init__(self,*args):  
        ex.__init__(self,*args)
    def __cmp__(self,other):
        raise NotImplementedError,"expr.__cmp__() should not be called"
    def __lt__(self, other):
        return relational(self, other, relational.less)
    # also other richcomp methods are defined but not shown here

then in Python (2.1b1) I get:

>>> expr(2) < expr(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 5, in __cmp__
NotImplementedError: expr.__cmp__() should not be called

I checked also the following approach:
class expr:
    def __init__(self,*args):
        self.e = ex(*args)
    def __cmp__(self,other):
        raise NotImplementedError,"expr.__cmp__() should not be called"
    def __lt__(self, other):
        return relational(self.e, other, '<')
    # also other richcomp methods are defined but not shown here

that works fine but then I would have to re-define also all `ex' methods
in `expr' --- there are more than 30 of them, so I would not want to do
that. Especially, if in future boost.python will support rich-comparisons.

I appreciate any hints how to wrap classes with no rich-comparisons with
classes that add such fuctionality.

Thanks,
	Pearu





More information about the Python-list mailing list