问题描述
我正在尝试将Python代码移植到Cython.我对C的经验非常有限.我正在尝试制作一个相对简单的类,用于存储多维数组.出于这个问题的目的,让我们将其留给属性时间为长度为1的单个一维数组.目前,我收到此错误:
I am making my first attempt at porting Python code to Cython. I only have very limited experience with C. I am trying to make a relatively simple class that stores multidimensional arrays. For the purpose of this question, let's leave it to a single one-dimensional array of length 1 for the attribute time. Currently, I am recieving the error:
cdef np.ndarray[np.int64_t, ndim=1] time = np.empty([1], dtype=np.int64)
^
------------------------------------------------------------
data.pyx:22:40: Cannot assign default value to fields in cdef classes, structs or unions
以下是最相关的文件.
data.pyx
import numpy as np
cimport numpy as np
cdef extern from "data_extern.h":
cppclass _Data "Data":
np.int64_t time64[1]
double x_coord, y_coord, z_coord
_Data(np.int64_t *time, double x, double y, double z)
cdef class Data:
cdef _Data *thisptr
cdef np.ndarray[np.int64_t, ndim=1] time = np.empty([1], dtype=np.int64)
def __cinit__(self, np.int64_t time[1], x, y, z):
self.thisptr = new _Data(&time[0], x, y, z)
def __dealloc__(self):
del self.thisptr
data_extern.h
data_extern.h
#ifndef DATA_EXTERN_H
#define DATA_EXTERN_H
class Data {
public:
signed long long time64[1];
double x_coord, y_coord, z_coord;
Data();
Data(signed long long time[1], double x, double y, double z;
~Data();
};
#endif
data_extern.cpp
data_extern.cpp
#include <iostream>
#include "data_extern.h"
Data::Data () {}
// Overloaded constructor
Data::Data (signed long long time[1], double x, double y, double z {
this->time64 = time[0];
this->x_coord = x;
this->y_coord = y;
this->z_coord = z;
}
// Destructor
Data::~Data () {}
我认识到我的代码可能存在多个问题,但是如果有人可以为错误消息提供解释,将不胜感激.
I recognize there could be multiple problems with my code, but if someone could provide an explanation for the error message it would be much appreciated.
推荐答案
问题如错误消息中所述:无法为cdef类的C级属性设置默认值.您可以通过在 __ cinit __
构造函数中设置值来解决此问题,如下所示:
The problem is as stated in the error message: you cannot set a default value for a cdef class's C-level properties. You can get around this by setting the value in the __cinit__
constructor as follows:
cdef class Data:
cdef _Data *thisptr
cdef np.ndarray[np.int64_t, ndim=1] time
def __cinit__(self, np.int64_t time[1], x, y, z):
self.thisptr = new _Data(&time[0], x, y, z)
self.time = np.empty([1], dtype=np.int64)
def __dealloc__(self):
del self.thisptr
不过,请注意,整个np.ndarray语法已经过时了.除非您对使用numpy类型感到无所适从(由于与某种C ++库接口,在代码中这似乎是不必要的),否则可以使用更现代的输入的memoryview 语法.您可以使用libc.stdint cimport * 中的导入大小为整数的类型,以使用这些类型而不是numpy的类型.
As a heads-up though, the whole np.ndarray syntax is rather out of date. Unless you are dead-set on using numpy types (which seem to be unnecessary in your code since you are interfacing with a c++ library of some sort), you can use the more modern typed memoryview syntax. You could import the sized integer types using from libc.stdint cimport *
to use those instead of the numpy ones.
这篇关于Cython“无法将默认值分配给cdef类,结构或联合中的字段"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!