Python Cython问题,can';不要在类变量上使用cdef
我正在尝试使用Cython将python类转换为C,以提高时间复杂度。我最常用的变量是init方法中定义的类变量,因此我想将它们定义为cdef double。我已经尝试了我能找到的一切,但什么都没有让我转换代码。似乎这应该是解决问题的方法:Python Cython问题,can';不要在类变量上使用cdef,python,c,python-3.x,cython,cythonize,Python,C,Python 3.x,Cython,Cythonize,我正在尝试使用Cython将python类转换为C,以提高时间复杂度。我最常用的变量是init方法中定义的类变量,因此我想将它们定义为cdef double。我已经尝试了我能找到的一切,但什么都没有让我转换代码。似乎这应该是解决问题的方法: class Wall(object): cdef double min_x, max_x, a, b, c def __init__(self, start, stop): self.min_x = min(start[0], stop[0])
class Wall(object):
cdef double min_x, max_x, a, b, c
def __init__(self, start, stop):
self.min_x = min(start[0], stop[0])
self.max_x = max(start[0], stop[0])
self.a = (stop[1]-start[1])/(stop[0]-start[0])
self.b = -1
self.c = start[1]-self.a*start[0]
但我得到了以下错误:
我做错了什么
您好,Jakob您需要公开类变量,并为类提供一个C声明。试试这个:
cdef class Wall(object):
cdef public double min_x, max_x, a, b, c
def __init__(self, start, loop):
...
我投票认为这是一个打字错误,因为它应该是
cdef类墙
,也就是说cdef
被遗忘了。
cdef class Wall(object):
cdef public double min_x, max_x, a, b, c
def __init__(self, start, loop):
...