Cython cdef类不显示文档字符串或_init_________________参数 我使用CDEF定义了一个Python类,它用Cython来封装C++类,并且它工作正常。但是,当我在python或类中使用帮助(类)时?在ipython中,我得到如下信息: >>> TestClass.CTestClass? Init signature: TestClass.CTestClass() Docstring: <no docstring> File: ~/Coding/CythonWithCppTutorials/ClassCythonCPPWithMemberFunctions/TestClass.so Type: type 和C++代码看起来是这样的:

Cython cdef类不显示文档字符串或_init_________________参数 我使用CDEF定义了一个Python类,它用Cython来封装C++类,并且它工作正常。但是,当我在python或类中使用帮助(类)时?在ipython中,我得到如下信息: >>> TestClass.CTestClass? Init signature: TestClass.CTestClass() Docstring: <no docstring> File: ~/Coding/CythonWithCppTutorials/ClassCythonCPPWithMemberFunctions/TestClass.so Type: type 和C++代码看起来是这样的:,python,c++,wrapper,cython,Python,C++,Wrapper,Cython,TestClassC.cpp #include <iostream> class TestClass{ public: TestClass(int Dimensionality, double* InputArray); // prototype of constructor ~TestClass(void); // prototype of destructor double SumListOfNumbers(void); int Dimensionality;

TestClassC.cpp

#include <iostream>

class TestClass{
public:
  TestClass(int Dimensionality, double* InputArray); // prototype of constructor
  ~TestClass(void); // prototype of destructor
  double SumListOfNumbers(void);
  int Dimensionality;
  double* ListOfNumbers;
};

TestClass::TestClass(int DIM, double* InputArray)
{
  Dimensionality = DIM;
  std::cout << Dimensionality << "\n";
  ListOfNumbers = new double[Dimensionality];
  for (int i = 0; i < Dimensionality; ++i) {
    ListOfNumbers[i] = InputArray[i];
    std::cout << ListOfNumbers[i] << ", ";
  }
  std::cout << "\n";
};

TestClass::~TestClass(void){
  std::cout << "Being Destroyed" << "\n";
};

double TestClass::SumListOfNumbers(void){
  double Sum = 0;
  for (int i = 0; i < Dimensionality; ++i) {
    Sum += ListOfNumbers[i];
  }
  return Sum;
}
#包括
类TestClass{
公众:
TestClass(整数维,双*输入阵列);//构造函数的原型
~TestClass(void);//析构函数的原型
数字的双重总和(无效);
内维度;
双*数字列表;
};
TestClass::TestClass(整型,双*输入阵列)
{
维度=DIM;

std::cout解决这个问题的方法是按照oz1的建议,将
embedsignature
指令设置为
True
,并添加一个普通的python
\uuuu init\uuuu
函数,如下所示:

@cython.embedsignature(True)
cdef class CTestClass: # defines a python wrapper to the C++ class
    """
    This is a test class wrapper for c++.
    """
    def __init__(self, Dimensionality, InputArray):
        pass

    cdef TestClass* thisptr # thisptr is a pointer that will hold to the instance of the C++ class


    def __cinit__(self, int Dimensionality, np.ndarray[double, ndim=1, mode="c"] InputArray not None): # defines the python wrapper class' init function
        cdef double[::1] InputArrayC = InputArray # defines a memoryview containnig a 1D numpy array - this can be passed as a C-like array by providing a pointer to the 1st element and the length
        self.thisptr = new TestClass(Dimensionality, &InputArrayC[0]) # creates an instance of the C++ class and puts allocates the pointer to this
然后init签名自动包含在docstring中,如下所示:

In [1]: import TestClass

In [2]: TestClass.CTestClass?
Init signature: TestClass.CTestClass(self, /, *args, **kwargs)
Docstring:
CTestClass(Dimensionality, InputArray)

This is a test class wrapper for c++.
File:           ~/Coding/CythonWithCppTutorials/ClassCythonCPPWithMemberFunctions/TestClass.so
Type:           type

将cython编译器指令
embedsignature
设置为
True
。请参阅使用cython 0.25.2简化的测试用例上的@SomeRandomPhysicator,它对我有效。我知道,将编译器指令embedsignature设置为True对docstring没有什么帮助,但它没有设置Init签名。
In [1]: import TestClass

In [2]: TestClass.CTestClass?
Init signature: TestClass.CTestClass(self, /, *args, **kwargs)
Docstring:
CTestClass(Dimensionality, InputArray)

This is a test class wrapper for c++.
File:           ~/Coding/CythonWithCppTutorials/ClassCythonCPPWithMemberFunctions/TestClass.so
Type:           type