`cpdef`: Bridging Python and C Efficiency

`cpdef` functions are uniquely designed to automatically create both a fast C function for internal Cython calls and a Python-callable wrapper, enabling seamless and efficient interoperability between Python and C codebases.

The top 3

  1. `cpdef`'s Dual Nature: Bridging Python and C Calls: `cpdef` functions generate two versions: a fast C-callable function for internal Cython/C calls and a Python-callable wrapper, enabling seamless invocation from both native C and Python codebases.
  2. `cpdef` vs. `cdef`: The Minor Speed Cost for Python Compatibility: While `cpdef` functions offer C-level speed, they introduce a slight calling overhead, often less than a nanosecond, compared to pure `cdef` functions due to internal checks for Python overriding and dynamic binding when handling Python objects.
  3. `cpdef`'s Automatic Type Coercion: Data Bridge to C: A key aspect of `cpdef`'s efficiency is its automatic type coercion, allowing Python objects (like lists or strings) to be seamlessly converted to their C equivalents (e.g., `std::vector` or `char*`) when called from C, and vice-versa, significantly reducing manual boilerplate.

Sources

Open the full topic