3

I defined a extension types with an array and a float number. But it goes wrong when I try to compile it: Cannot convert Python object argument to type 'float *'. It seems that I couldn't pass a pointer to constructor. Is there any solution to avoid this problem?

cdef class Particle(object):
    cdef float pos[3]
    cdef float D

    def __init__(self,float pos[3],float D):
        self.pos=pos
        self.D=D
alf
  • 35
  • 6

1 Answers1

2

I think a C array is not allowed there, tuple or list might work.

cdef class Particle(object):
    cdef float pos[3]
    cdef float D

    def __init__(self, tuple pos, float D):
        self.pos = pos
        self.D = D

The code produced by cython seems sensible,

if (unlikely(__Pyx_carray_from_py_float(__pyx_v_pos, __pyx_t_1, 3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  memcpy(&(__pyx_v_self->pos[0]), __pyx_t_1, sizeof(__pyx_v_self->pos[0]) * (3));
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • so, cython only allow python and basic C data type to be passed to constructor not pointer. Thanks – alf Jan 22 '16 at 17:01