Skip to content

Cython: add more Pythonic way of making type annotations #340

@ashwinvis

Description

@ashwinvis

Use Pure Python versions of Cython

We can try to teach the Pure Python syntax of Cython instead of the cdef syntax.

Pure Python syntax which allows static Cython type declarations in pure Python code, following PEP-484 type hints and PEP 526 variable annotations.

https://cython.readthedocs.io/en/stable/src/userguide/language_basics.html

Typed memoryviews syntax for Numpy

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.

https://cython.readthedocs.io/en/stable/src/userguide/memoryviews.html#memoryviews

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions