top | item 37737745

(no title)

thanatropism | 2 years ago

What are the advantage of Cython over something like C++ with pybind11 or whatever the equivalent in Rustland?

discuss

order

claytonjy|2 years ago

Cython is similar to python and is fairly easy to write for a python user, while C++ or Rust have much larger learning curves.

ynik|2 years ago

However if one is using C++/Rust anyways, then it's a good idea to stay away from Cython. From afar, Cython seems like a viable solution for Python/C++ interop. But the details get messy: you need to clone the .h headers into .pxd Cython-readable headers; and more advanced template-magic C++ constructs may end up being not directly usable in Cython due to missing features or bugs in the C++ support.

In the end, we ended up with quite a number of layers wrapping each other:

  1. actual C++ implementation
  2. actual C++ header
  3. C++ wrapper implementation, avoiding constructs that Cython doesn't support
  4. C++ wrapper header
  5. Cython .pxd for step 4
  6. Cython .pyx exposing `cdef class`es to Python with a nice Python-style API for the original C++ library.
  7. Hand-written .pyi for type checking the remaining Python code, because Cython doesn't have support for auto-generating these yet.
Had we used pybind11 / nanobind instead, we could have stopped at step 3. Cython started easy, but ended up being a major maintenance burden.

nurettin|2 years ago

It lets you refactor your code to line by line.