0
cdef class Y:

    cdef double* _ptr

    def __cinit__(self, double* ptr):
        self._ptr = ptr

cdef class X:

    cdef double* _ptr

    def __cinit__(self):
        # some magic with _ptr
        self._y = Y(self._ptr)
        

Cannot convert double * to Python object

Why does this happen? Is there a nicer way of fixing this other than passing the whole X object to Y and extracting _ptr by hand?

Raymond Toh
  • 779
  • 1
  • 8
  • 27
kebabdubai
  • 67
  • 6
  • Does this answer your question? [Cython: "Cannot convert Python object" error](https://stackoverflow.com/questions/47005382/cython-cannot-convert-python-object-error) – Hamidreza Khorammfar Sep 29 '21 at 15:26
  • Not really, my question is specifically about cdef classes with __cinit__ constructors, which in theory should support passing cdef arguments somehow – kebabdubai Sep 29 '21 at 15:28
  • Unfortunately I’m afraid that the answer to my question is that __cinit__ is def not cdef, so there is no elegant, no-overhead way to solve it – kebabdubai Sep 29 '21 at 15:31
  • The usual way to solve this is to make a `cdef` staticmethod (e.g. https://stackoverflow.com/questions/19370230/cython-overloaded-constructor-initialization-using-raw-pointer) – DavidW Sep 29 '21 at 17:46
  • Wouldn’t classmethod be even more suitable? – kebabdubai Oct 01 '21 at 10:35
  • I don't think classmethod is supported for cdef functions – DavidW Oct 14 '21 at 12:47

0 Answers0