How to keep a C++ class name unmodified with Cython?
Question: I have a C++ class called Foo. If I follow the [Cython C++
tutorial](http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html) 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.


Answer: There are two ways to handle this.

  1. Declare C++ class with an alternate name; original name has to be specified in double quotes:
    
        cdef extern from "defs.h" namespace "myns":
        cdef cppclass CMyClass "myns::MyClass":
            ...
    

Then you can use `MyClass` for your python class and refer to C++ declaration
as `CMyClass`.

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.

  2. Declare your C++ classes in a separate `.pxd` file, named differently from your `.pyx` file, then import them with `cimport`.

In cpp_defs.pxd:

    
        cdef extern from "defs.h" namespace "myns":
        cdef cppclass MyClass:
            ...
    

In py_wrapper.pyx:

    
        cimport cpp_defs as cpp
    
    cdef class MyClass:
        cpp.MyClass *_obj
    

