I am converting a C++ plugin to Python and I ran into several operator
overloading functions. I understand how they work in C++ but what is
the best way to mimic them in python. My only thought right now is to
write a Python definition like this, which means I need to find every
operator in the file and make it use a function instead.

def addStuff(self, a, b):
        c = a + b
        return c


Here are the C++ functions:


double* MatrixNN::operator[](unsigned i)
{
        double *ptr = ptrDbl + (i*uDimension) ;
        return ptr ;
}

const double* MatrixNN::operator[](unsigned i) const
{
        double *ptr = ptrDbl + (i*uDimension) ;
        return ptr ;
}

double& MatrixNN::operator()(unsigned i, unsigned j)
{
        unsigned idx = getIndex(i, j) ;
        return (ptrDbl[idx]) ;
}

double MatrixNN::operator()(unsigned i, unsigned j) const
{
        unsigned idx = (i*uDimension) + j ;
        return (ptrDbl[idx]) ;
}

ostream& operator<<(ostream &os, const MatrixNN &matNN)
{
        os << "MatrixNN " << matNN.uDimension << "x" << matNN.uDimension <<
endl ;
        if (matNN.uDimension > 0 && matNN.ptrDbl != NULL)
                {
                unsigned i,j;
                for (i=0; i < matNN.uDimension; ++i)
                        {
                        os << "ROW_" << i << "= " ;
                        for (j=0; j < matNN.uDimension; ++j)
                                {
                                os << matNN[i][j] << " " ;
                                }
                        os << endl ;
                        }
                }
        return os ;
}

MatrixNN& MatrixNN::operator=(const MatrixNN &matNN)
{
        setDimension( matNN.uDimension );

        unsigned u;
        unsigned uTotal = uDimension * uDimension ;
        for (u=0; u < uTotal; ++u)
                ptrDbl[u] = matNN.ptrDbl[u] ;

        return (*this) ;
}
--~--~---------~--~----~------------~-------~--~----~
Yours,
Maya-Python Club Team.
-~----------~----~----~----~------~----~------~--~---

Reply via email to