You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cython: add more Pythonic way of making type annotations #340
Also for Numpy arrays we can type declare using data: cython.int[:,:] instead of cdef cnp.ndarray[cnp.int32_t, ndim=2] data. Yes it is not equivalent, but it is elegant and easier to teach.
Typed memoryviews allow efficient access to memory buffers, such as those underlying NumPy arrays, without incurring any Python overhead. Memoryviews are similar to the current NumPy array buffer support (np.ndarray[np.float64_t, ndim=2]), but they have more features and cleaner syntax.
The Pure Python syntax seems considerably more restricted in what you can do with it. From their docs:
Although it is not typically recommended over writing straight Cython code in a .pyx file, there are legitimate reasons to do this - easier testing and debugging, collaboration with pure Python developers, etc. In pure mode, you are more or less restricted to code that can be expressed (or at least emulated) in Python, plus static type declarations. Anything beyond that can only be done in .pyx files with extended language syntax, because it depends on features of the Cython compiler.
I can see it being good for getting started with Cython though, and since this is an introductory course it could make sense to translate all current examples to it (no biggie, we only have a few). I would add some form of the above quotation as a caveat though, and recommend the .pyx approach for "real" projects. Perhaps do this for 2027?
Memoryviews are definitely nice. I'll see if I can incorporate them in the current material already now. Incorporated now in #347
Activity
ashwinvis commentedon Nov 27, 2025
As a side note, in an @ENCCS lesson about Cython, we are already doing this:
https://github.com/ENCCS/python-for-hpc/blob/fcaebd3aa5db760039c863823a8eb93d00240a29/content/accel-cython.rst
niemilau commentedon Nov 24, 2026
The Pure Python syntax seems considerably more restricted in what you can do with it. From their docs:
I can see it being good for getting started with Cython though, and since this is an introductory course it could make sense to translate all current examples to it (no biggie, we only have a few). I would add some form of the above quotation as a caveat though, and recommend the
.pyxapproach for "real" projects. Perhaps do this for 2027?Memoryviews are definitely nice.
I'll see if I can incorporate them in the current material already now.Incorporated now in #347ashwinvis commentedon Nov 25, 2026
Agree with you. Unfortunately Cython is so complex that it cannot optimize itself, and these different annotations give different performances.
ashwinvis commentedon Nov 25, 2026
More or less fixed in #347