Thanks chad thats exactly what I was looking for!
On Feb 10, 11:35 am, chadrik <[email protected]> wrote:
> check out this doc:
>
> http://www.python.org/doc/2.5.2/ref/numeric-types.html
>
> [] __getitem__
> () __call__
> >> __rshift__
> << __lshift__
>
> the only thing from this list that i see you cannot do is overload the
> equal (=).
>
> you can't overload foo = 'bar', but you can overload foo.spangle =
> 'bar' using __setattr__.
>
> the pythonic way of doing ``foo='bar'`` is ``foo = Foo('bar')``, in
> which case you handle your logic in __new__ or __init__ of Foo.
>
> -chad
>
> On Feb 10, 2009, at 9:59 AM, ryant wrote:
>
>
>
> > 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.
-~----------~----~----~----~------~----~------~--~---