aerijman
aerijman

Reputation: 2762

Is it possible to pass a numpy array by reference into a cpdef function?

Is it possible to write a Cython function where a numpy array is passed by reference (perhaps a memory view?) and can I use such function in Python?

I tried:

cpdef void my_function(bytes line, int& counter, np.ndarray[np.uint32_t, ndim=2]& sums):
    ...
    ...

Here counter and sums would be passed by reference. I am interested in the latter (but will happily accept critics and suggestions regarding both).

The compiler throws:

Reference base type cannot be a Python object

I get the same message with cdef either and I don't understand the full etiology of the problem.

Upvotes: 0

Views: 474

Answers (1)

DavidW
DavidW

Reputation: 30890

Yes - sort of.

A Numpy array is a Python object. All Python objects are passed "by reference" anyway (i.e. if you change the contents of a Numpy array inside the function you change it outside).

Therefore you just do

cpdef void my_function(bytes line, int& counter, np.ndarray[np.uint32_t, ndim=2] sums):

Specifying it as a C++ reference doesn't really make much sense. For a Python object there's quite a bit of detail that Cython hides from you.


However, cpdef functions are designed to be callable from Python and Cython. Python ints (and other numeric types) are immutable. Therefore changing counter will not work as you think when called from Python, and if it did what you hoped it'd potentially break the interpreter.

I'd therefore avoid using references in cpdef functions (and probably mostly avoid cpdef functions completely - it's usually better just to decide on def or cdef)

Upvotes: 2

Related Questions