我在cython中有一个cdef类,我想用setattr内置函数初始化它的字段。然而,当我这样做的时候,我得到了一个执行错误:
/path/.../cimul.cpython-34m.so in cimul.Simulation.__init__ (cimul.c:5100)()
AttributeError: 'Simulation' object has no attribute 'Re'我的代码如下:
cdef class Simulation:
cdef double Re, Pr, Ra, a, dt_security
cdef int Nz, NFourier, freq_output, freq_critical_Ra, maxiter
cdef bool verbose
def __init__(self, *args, **kargs):
param_list = {'Re': 1, 'Pr': 1, 'Ra': 1, 'a' : 1, 'Nz': 100,
'NFourier': 50, 'dt_security': 0.9,
'maxiter': 100, 'freq_output': 10,
'freq_critical_Ra':50, 'verbose': False}
# save the default parameters
for param, value in param_list.items():
setattr(self, param, value)你知道我怎么才能绕过这个问题吗?
发布于 2014-06-03 04:35:51
cdef public,他们会添加一些property访问函数,该函数不仅允许从Python访问,还可以在C结构中保留关联标识符<-->偏移量。通过这些属性函数会产生一些开销。另请注意,这些函数执行Python类型检查/转换。现在回答您的问题,您需要以某种方式保持关联标识<-->偏移量。如果你想让事情变得更快,唯一的办法就是手工完成:
self.RE = param_list['RE'] # self.RE is a C struct access
self.Pr = param_list['Pr']
...https://stackoverflow.com/questions/23986606
复制相似问题