Cython and constructors of classes

天涯浪子 提交于 2019-12-04 12:42:26

The issue here is that you can't overload __cinit__() like that (as only cdef functions can be overloaded) - instead, have it take default values, then call the right thing as needed.

Edit: Essentially, you need to implement the function in a way closer to how you would in normal Python code, instead of using overloading:

def __cinit__(self, double val1=-1, double val2=-1): 
    if val1 == -1 or val2 == -1:
        self.thisptr = new Node()
    else:
        self.thisptr = new Node(val1,val2)

Naturally, this presumes -1 is a value that isn't useful for the function, you could use another value, or if you need every value of a double to be valid, then you might need to remove the typing, taking a Python object so that you can use None as the default:

def __cinit__(self, val1=None, val2=None):
    if val1 is not None and val2 is not None:
        self.thisptr = new Node(val1, val2)
    else:
        self.thisptr = new Node()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!