Cython Trouble

Here are a couple of things I experienced using Cython to wrap my C++ library grakopp:

Assignment and Coercion

  • I couldn’t find a nice way to wrap boost::variant. Although the direct approach works, an assignment to the variant requires an unsafe cast, but that also adds the overhead of a copy. To work around this, I used accessor functions (requires changing the C++ implementation).
  • The operator= is not supported to declare custom assignment functions.
  • There is no other way to add custom coercion rules. The support for STL container coercion is hardcoded in Cython/Compiler/ This also makes the builtin coercions less useful.
  • string coercion seems to be unhappy quite often, so you have to cast to string or char* even constant strings.


  • Relative cimports are not supported.
  • Unintuitively, a corresponding pxd file is automatically included, which can not be supressed. So renaming its imports with “as” in a cimport is not possible.
  • There is no sensible place to put shared pxd files. [Wiki] [Forum]

References and Initializers

  • References can not be initialized with an assignment, so pointers or slow copies have to be used everywhere.
  • Only the default constructor can be used to instantiate stack or instance variables.


  • Cython could not resolve function overloading which differed only in the constness of the return type.


  • It’s not possible to write meta-extension classes for template classes directly – only extension types for specific instantiations. [Forum]
  • It’s not possible to use non-type template arguments (as a workaround, you can use ctypedef int THREE "3"). [Forum]
  • It’s also not possible to instantiate function templates, but there is a similar workaround. [Forum]