我对 Cython 使用默认构造函数有疑问。
我的 C++ 类 Node 如下
Node.h
class Node
{
public:
Node()
{
std::cerr << "calling no arg constructor" << std::endl;
w=0.0;
d=0.0;
}
Node(double val, double val2);
{
std::cerr << "calling 2 args constructor" << std::endl;
this->w=val;
this->d=val2;
}
private:
double d,w;
}
用Cython包装如下
cdef extern from "Node.h":
cdef cppclass Node:
Node() except +
Node(double val1, double val2) except +
double d
double w
cdef class pyNode:
cdef Node *thisptr # hold a C++ instance which we're wrapping
def __cinit__(self):
self.thisptr = new Node()
def __cinit__(self, double val1, double val2):
self.thisptr = new Node(val1,val2)
def __dealloc__(self):
del self.thisptr
def __repr__(self):
return "d=%s w=%s" % (self.thisptr.w, self.thisptr.w )
Cython 代码编译良好,尤其是从 Python 调用时
from pyNode import pyNode as Node
n=Node(1.0,2.0)
我得到了预期的calling 2 args constructor
字符串,但如果我尝试使用“无参数”构造函数从 python 声明 Node 对象(应正确声明为__cinit__(self)
我没有得到任何输出,这意味着没有调用无参数构造函数!
我怎样才能明确地从cinit包装类的方法?
这里的问题是你不能超载__cinit__()
像那样(如only cdef函数可以重载 https://github.com/cython/cython/wiki/enhancements-overloading) - 相反,让它采用默认值,然后根据需要调用正确的东西。
编辑:本质上,您需要以更接近普通 Python 代码的方式实现该函数,而不是使用重载:
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)
自然地,这假设-1
是一个对函数没有用的值,您可以使用另一个值,或者如果您需要 double 的每个值都有效,那么您可能需要删除键入,采用 Python 对象,以便您可以使用None
作为默认值:
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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)