Cannot convert Python object argument to type 'double *'

839 views
Skip to first unread message

Zahra Sheikh

unread,
Sep 6, 2017, 7:31:41 AM9/6/17
to cython-users
Hi everyone,

I want to initialize some pointers as argument in a cython class 

import cython
import numpy as np
import ctypes
cimport numpy as np
cdef class myClass:
      cdef int ns, m
      cdef double emax
      cdef double* x
      cdef double* hx
      cdef double* hpx
      cdef bint lb, ub
      cdef int ifault
      cdef int* iwv
      cdef double* rwv
      def __cinit__(self, int ns, int m, double emax, double* x, double* hx, double* hpx, int* iwv, double* rwv, bint lb = False, bint ub = False, int ifault = 0):

When I compile my code I get the following error message:

 Cannot convert Python object argument to type 'double *'

How can I pass the pointers as argument to a function?

Thanks in advance.

Regards,
Zahra



Stefan Behnel

unread,
Sep 6, 2017, 7:45:38 AM9/6/17
to cython...@googlegroups.com
Hi!
First of all, it's a method, not a function. Specifically, "__cinit__" is
part of the constructor, i.e. all arguments come from a Python call that
instantiates the class. You cannot pass anything from Python that is
compatible with a "double*".

I recomment instantiating your class with an internal cdef factory
function, and not pass the arguments into the constructor but assign them
after the creation. If it's a purely internal class, consider declaring it
as "@cython.internal", and maybe also "@cython.final" if you don't want to
allow inheritance.

Stefan

Zahra Sheikh

unread,
Sep 6, 2017, 8:10:38 AM9/6/17
to cython-users, stef...@behnel.de
Thanks Stefan for your suggestion and I apologize for my mistake. Could you please give a simple example of how it should be done?

Zahra
Reply all
Reply to author
Forward
0 new messages