我在 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)
你知道我该如何规避这个问题吗?