0

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.

2
  • Tell us about the array? shape, and especially dtype. Commented Nov 12, 2020 at 19:31
  • @hpaulj unsigned integers and shape = (76 x 74) Commented Nov 12, 2020 at 19:43

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.