I have a C++ class called Foo. If I follow the Cython C++ tutorial I will need to call the Python class differently, PyFoo for example.
However I really need to call the Python class Foo as well. How to do that efficiently?
Edit: I’m trying to interface an existing C++ library that was previously interfaced with Boost Python. For different reasons I would like to test Cython instead.
Since with Boost:Python Python classes were called with the same name as in C++, I would like to continue with this naming convention. It’s not a Python (CPython) requirement to call classes differently, but it seems to be imposed by Cython, at least in the tutorial.
I can of course use a pure python module to define a Foo class that calls PyFoo, but this seems both boring and inefficient.
There are two ways to handle this.
Declare C++ class with an alternate name; original name has to be specified in double quotes:
Then you can use
MyClassfor your python class and refer to C++ declaration asCMyClass.Note that original name has to include the namespace explicitly (if it is namespaced).
Cython template arguments (when present) should go after an alternate name declaration.
Declare your C++ classes in a separate
.pxdfile, named differently from your.pyxfile, then import them withcimport.In cpp_defs.pxd:
In py_wrapper.pyx: