Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining a static array #3161

Open
jakirkham opened this issue Oct 1, 2019 · 3 comments

Comments

@jakirkham
Copy link
Contributor

commented Oct 1, 2019

Naively would expect the following code to return something like int* or similar, but was surprised to find it returned list. Is this expected? If so, how should one define a static array of fixed size?

cpdef void func(): 
    cdef int[2] a 
    print(type(a)) 
@TeamSpen210

This comment has been minimized.

Copy link
Contributor

commented Oct 2, 2019

It's not a Python object, it's a C type. Cython though has a conversion which creates a
Python list holding Python versions of the contents (so a bunch of int objects), which it runs when you pass it to the Python type function here.

@jakirkham

This comment has been minimized.

Copy link
Contributor Author

commented Oct 2, 2019

It's not a Python object, it's a C type.

That’s what I gathered from looking at the generated C code.

Cython though has a conversion which creates a Python list holding Python versions of the contents (so a bunch of int objects), which it runs when you pass it to the Python type function here.

Ok. I must have missed this. Though why are we also not able to create a memoryview from its content? ( #3162 ) Or more generally, when does Cython decide to use a list and how can we prevent it from doing this?

@jakirkham

This comment has been minimized.

Copy link
Contributor Author

commented Oct 2, 2019

To answer my own question, one can construct a memoryview onto the static array like so. Though others may have better ideas on how to do this.

cpdef void func():
    cdef int[2] a
    cdef int[::1] m = <int[:2:1]>(&a[0])
    print(type(m))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.