Skip to content Skip to sidebar Skip to footer

In C++/cython It Possible To Only Declare Relevant Attributes To Be Visible In Python?

Say I have a .h file with the following code: class MyClass { public: int Attribute1; int Attribute2; MyClass(){}; virtual ~MyClass(){};

Solution 1:

cdefclass PyClass:
    cdefMyClass *classptr
    # ...
    cdefint Attribute1;

Attribute1 doesn't do what you think. It's a separate value stored as part of PyClass and has nothing to do with the Attribute1 in classptr. You probably want to write a property instead.


However, to answer your question:

Yes, it's fine for you to only wrap the specific functions you're interested in. Cython doesn't need to know all the details of your C++ classes - it only needs to know enough details to generate valid C++ code using them. A few quick examples of things it's useful to omit:

  • Templates. For example std::string is really a template typedef, but it may not be necessarily for a Cython wrapper to know this, or for optional allocator template types, or for numeric template types which Cython doesn't really support.

  • Complicated inheritance hierarchies: doesn't matter if the functions you care about actually come from a base-class. Just wrap the derived class you're using.

  • Interfaces that return other classes - because then you'd need to wrap the second class (which might expose a third class...).

There really is no consequence beyond not being able to call the code you haven't wrapped from python. Cython's C++ support is (and will likely always be) somewhat incomplete and it's often necessary to give it a simplified C++ interface to get anything done.

Post a Comment for "In C++/cython It Possible To Only Declare Relevant Attributes To Be Visible In Python?"