Hi List,
sorry for the crosspost, I have also asked the question here https:// answers.launchpad.net/cython/+question/31930, but probably the list is a better place to ask.


In the new version of Cython I am excited to report that overloading operators seems to work just fine. This is great, because in the high energy physics community, people think it's a good idea to abuse the () operator for iterators to mean next (among other nonsense).

So I have a short class in C++, the code is below, and as posted, everything works fine. However, when I try to change "def add(self)" to "def __add__(self)" in myTest I get complaints of the sort

  File "test.py", line 37, in <module>
    print test + 3
File "stdMatrix.pyx", line 67, in stdMatrix.myTest.__add__ (stdMatrix.cpp:452)
    return self.thisPtr.add(<int>other)
AttributeError: 'stdMatrix.myTest' object has no attribute 'thisPtr'

I find this quite surprising, and could use some enlightenment. I can easily enough work around this, but this is a bug, no?

Thanks for cython and best of luck on your sprint.
Best,
    Jan

------------ class_operators.cc ---------------
class TEST
{
public:
    int x;
    TEST(int y) {x=y;}
    int operator() () {x = 17; return x;}
    int operator+(int y) {return x+y;}
    int minimize() {return 25;}
};
----------------------------------------------------------

------stdMatrix.pyx----------------------------------------
cdef extern from "class_operators.cc":
    ctypedef struct monkey "TEST":
        int x
        int (*minimize)()
        int add "operator+"(int y)
        int call "operator()"()
    monkey* new_Test "new TEST"(int y)
    void del_Test "delete"(monkey* x)

cdef class myTest:
    cdef monkey *thisPtr
    def __cinit__(self, int y):
        self.thisPtr = new_Test(y)
    def __dealloc__(self):
        del_Test(self.thisPtr)
    def min(self):
        return self.thisPtr.minimize()
    def plus(self, other):
        return self.thisPtr.add(other)
    def call(self):
        return self.thisPtr.call()
--------------------------------------------------------------------

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to