Cython cdef Class not displaying doc string or __init__ parameters

只愿长相守 提交于 2019-12-09 05:49:28

The way to solve this was to do as was suggested by oz1 and set the embedsignature directive to True and also to add a normal python __init__ function like so:

@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

Then the init signature is included in the docstring automatically like so:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!