- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试用 Cython 包装一些 C++ 类。对于基类,一切正常。对于具有与其各自基类相同参数(在其构造函数中)的派生类,也会发生同样的情况。但是,我无法实例化与其基类具有不同数量参数(在其构造函数中)的派生类。
例如:
// CYH.h file
#ifndef __CYH_H__
#define __CYH_H__
#include <iostream>
class Base
{
public:
Base(double);
~Base();
double A;
double getA();
void setA(double);
};
class Derived : public Base
{
public:
Derived(double, double);
~Derived();
double B;
};
#endif
// cyh.cpp file
#include <iostream>
#include <string>
#include "include/cyh.h"
Base::Base(double a)
{
A = a;
}
Base::~Base()
{
}
double Base::getA(){
return A;
}
void Base::setA(double a_){
A = a_;
}
Derived::Derived(double a, double b) : Base(a)
{
B = b;
}
Derived::~Derived()
{
}
cdef extern from "include/cyh.h":
cdef cppclass Base:
Base(double)
double getA()
void setA(double)
cdef cppclass Derived(Base):
Derived(double, double)
cdef class cyBase:
cdef Base *thisptr
def __cinit__(self, double a):
if type(self) is cyBase:
self.thisptr = new Base(a)
def __dealloc__(self):
if type(self) is cyBase:
del self.thisptr
@property
def A(self):
return self.thisptr.getA()
@A.setter
def A(self, value):
self.thisptr.setA(value)
cdef class cyDerived(cyBase):
cdef Derived *thisptrDerived
def __cinit__(self, double a, double b):
if type(self) is cyDerived:
self.thisptrDerived = self.thisptr = new Derived(a, b)
def __dealloc__(self):
if type(self) is cyDerived:
del self.thisptrDerived
它编译并且 pyd 构建成功。但是,当我尝试使用以下方法实例化 cyDerived 类时:
mycyObj = cyDerived(a=2., b=3.)
我收到以下错误:
__cinit__() got an unexpected keyword argument 'b'
Ant 知道我做错了什么?
非常感谢!
最佳答案
其实这是一个很有趣的问题:使用*args
、**kwargs
的性能损失是多少,应该考虑哪些场景?
从理论的角度来看,如果*args
,使用**kwargs
解决方案:
args
和kwargs
的指针并在垃圾收集器中注册这些引用,我的假设是,注册/取消注册可能是个问题,因为引用更新需要获取 GIL。最好不要经常这样做。
没有 *args
的解决方案似乎能够在不需要更改引用计数器的情况下工作。
到目前为止我的理论,但我希望看到一些实证结果,这可能会非常令人惊讶,所以如果您有一些数据,请与我们分享。
重现我的结果的代码:
#no_kwargs.pyx:
cdef class cyBase:
def __cinit__(self, double a):
pass
cdef class cyDerived:
def __cinit__(self, double a):
self.a=a
#kwargs.pyx
cdef class cyBase:
def __cinit__(self, *argv, **kwargs):
pass
cdef class cyDerived:
def __cinit__(self, double a):
self.a=a
运行它
python -m cython XXX.pyx
并比较生成的 c 文件。主要区别在于 cyBase::__cinit__()
整个引用计数发生的地方:
static int __pyx_pw_4test_6cyBase_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
...
__Pyx_INCREF(__pyx_args); // <----------- HERE
__pyx_v_argv = __pyx_args;
...
/* function exit code */
__Pyx_XDECREF(__pyx_v_argv); // <----------- HERE
...
return __pyx_r;
}
kwargs
这里好像没用到。
关于python - cython c++类继承无法实例化派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46342971/
c 不做边界检查。那么cython是如何检查是否编译成c的呢? %%cython --annotate cimport cython @cython.boundscheck(True) cpdef m
可以直接声明用于 Cython 构造函数? 据我了解,这是可能的: # Cython cdef int[3] li = [1, 2, 3] # C++ int[3] li = {1, 2, 3} 但
所以,如果你有一个头文件。 %%file test.h struct mystruct{ int i; int j; }; 然后你将它包装在 Cython 中: cdef extern fr
我正在构建一个独立于平台的 cython 项目,我想根据正在使用的编译器传递编译器参数。我可以猜测基于平台的编译器,或者假设它与用于 Python 的编译器相同,但不能保证匹配。通常我注入(injec
我使用诗歌构建我的 cython 包。我在所有函数和类中都有 NumPy 风格的文档字符串。我现在要做的是添加 Sphinx 自动文档并发布在 Read the Docs。 我已阅读此主题 How d
赛通 libcpp模块包含 priority_queue 的模板,这很好,除了一件事:我不能通过自定义比较器(或者,至少,我不知道如何)。 我需要这个,因为我需要 priority_queue做一个a
以下代码定义了一个简单的 Cython 函数(为方便起见,使用 Ipython 魔法)。 %load_ext cython %%cython def f(float x, float y=2):
我正在尝试使用 cython 进行复数计算。在示例代码中,我想计算复数的复指数函数。问题是我不知道如何将我的整数乘以虚数单位。python的虚数单位1.0j乘以cython执行时报错。 这是我的代码:
在这里停留在一些基本的 Cython 上 - 在 Cython 中定义字符串数组的规范且有效的方法是什么? 具体来说,我想定义一个定长常量数组char . (请注意,此时我不想引入 NumPy。) 在
是否有可能,如果是,如何确定 Cython 中整数数据类型的大小(以位为单位)? 我正在尝试做这样的事情,以获得整数大小: cdef WORD_BITS = 0 IF sizeof(unsigned
我只是想打印 cython 变量的地址,但我无法绕过错误消息: cdef int myvar print &myvar 抛出 Cannot convert 'int *' to Python obje
我有一个 C 头文件,它在宏中定义了一个函数。我需要从 Cython 调用它。有没有办法在 Cython 中使用宏并使其完全扩展?我已经有了 C 类型的参数。 我尝试像使用函数一样使用 cdef,我认
令人惊讶的是,我似乎找不到通过名称获取结构体元素的单个示例(无论是在网络上还是在 cython 示例中)。 所以我收到了一个指向 C 函数结构体的指针,并且想要一一访问这些元素并将它们重新打包到 py
我尝试围绕 C++ 库编写一个 Cython 包装器 http://primesieve.org/ 它包装了一个函数count。到目前为止,它可以正确安装 python setup.py instal
我正在尝试将 cython 模块 data.pyx 导入另一个 cython 模块 user.pyx。一切都编译得很好,但是当我尝试在 python 模块中调用 user.pyx 时,我收到错误“Im
更新:内存 View 获胜。Cython 使用类型化内存 View :0.0253449 特别感谢 lothario,他指出了几个关键的变化。 荒谬。当然现在的问题是,似乎不能对它们做太多算术(加法和
我有一个使用 memoryview 数组的 cython 模块,即... double[:,:] foo 我想使用多处理并行运行这个模块。但是我得到了错误: PicklingError: Can't
我正在尝试使用 Cython 加速 PEP 484 类型的 python 脚本。我想保持一些语义和可读性。 之前,我有一个 Flags = int def difference(f1: Flags,
这个问题已经有答案了: Collapse multiple submodules to one Cython extension (5 个回答) 已关闭 3 年前。 我在一个包中有多个 .py 文件
我已经能够在我的 .pyx 脚本上使用 cython 在 linux 上创建一个 .so 文件。我也可以成功地在我的 python 解释器上进行导入。 我的问题是如何在不使用 cython 的情况下将
我是一名优秀的程序员,十分优秀!