Cython class AttributeError

孤者浪人 提交于 2019-12-01 21:49:46

问题


I have started to experiment with Cython and ran into the following problem. Consider the following class representing a vertex in the 3D space:

#Vertex.pyx
cdef class Vertex(object):
    cdef double x, y, z
    def __init__(self, double x, double y, double z):        
        self.x = x
        self.y = y
        self.z = z

Now I try to create an object from the Python console:

import Vertex as vt
v1 = vt.Vertex(0.0, 1.0, 0.0) 

which works fine. However, when I'm trying to access the class attributes, I'm getting an AttributeError:

print v1.x
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-83d928d774b7> in <module>()
----> 1 print v1.x

AttributeError: 'Vertex.Vertex' object has no attribute 'x'

Any ideas why this could happen?


回答1:


By default cdef attributes are only accessible from within Cython. If you make it a public attribute with cdef public in front of the attribute name then Cython will generate suitable properties to be able to access it from Python.



来源:https://stackoverflow.com/questions/55230665/cython-class-attributeerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!