A skill for using Cython 3.2.4, an optimizing Python compiler that makes writing C extensions as easy as Python itself by translating Python code to C/C++ with support for calling C functions and declaring C types on variables and class attributes. Use when building high-performance Python extensions, wrapping C/C++ libraries, creating optimized numerical code with NumPy integration, implementing parallel algorithms with OpenMP, or needing fine-grained manual tuning from broad to low-level C optimizations.
A skill for using Cython 3.2.4, an optimizing Python compiler that makes writing C extensions as easy as Python itself by translating Python code to C/C++ with support for calling C functions and declaring C types on variables and class attributes. Use when building high-performance Python extensions, wrapping C/C++ libraries, creating optimized numerical code with NumPy integration, implementing parallel algorithms with OpenMP, or needing fine-grained manual tuning from broad to low-level C optimizations.
Cython 3.2.4
Overview
Cython is a compiler that translates Python-like code into optimized C or C++ extensions. It supports the full Python language plus optional static typing, C-level function calls, and direct access to C library APIs. Code runs within the Python runtime environment but compiles to native machine code instead of interpreted bytecode.
Cython's two syntax variants — Cython syntax (.pyx files with cdef, cimport) and Pure Python mode (.py files with cython.* annotations and decorators) — let you start with plain Python and incrementally add performance optimizations where needed.
Key capabilities:
Static type declarations for variables, function parameters, and return values
Direct C/C++ function calls with zero Python overhead
Typed memoryviews for efficient NumPy array access
Fused types for generic programming (like C++ templates)
OpenMP-based parallelism via prange
GIL release for true multi-threaded execution
Wrapping existing C and C++ libraries
When to Use
Building high-performance Python extensions where loops and numerical operations dominate runtime
Wrapping C or C++ libraries for use from Python
Optimizing hot paths in existing Python code with incremental typing
Creating typed NumPy operations that outperform pure NumPy vectorization
Implementing parallel algorithms using OpenMP (prange)
Needing fine-grained control from broad Python-level optimization down to manual C tuning
Core Concepts
Two compilation stages: A .pyx or .py file is first compiled by Cython into a .c or .cpp file, then compiled by a C/C++ compiler into a platform-specific shared library (.so on Linux, .pyd on Windows).
Pure Python mode (.py): Uses PEP-484 type annotations with cython.* types and @cython.* decorators. Valid Python that also compiles with Cython.
Three function types:
def — Python-callable function, always goes through Python calling convention
cdef / @cython.cfunc — C-only function, fastest calls within Cython code, not visible from Python
cpdef / @cython.ccall — Hybrid: callable from both Python and C, uses fast C calling when called from Cython
Extension types: Declared with cdef class or @cython.cclass, these are like Python classes but with faster attribute access for cdef members. They compile to C structs.
Installation / Setup
Install Cython via pip:
pip install "Cython==3.2.4"
A C compiler is required (gcc, clang, or MSVC). For NumPy integration, also install numpy.