Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

python - Cython class AttributeError

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


Some extra notes about related problems:

If you're getting the same error from within Cython then you have probably forgotten to tell Cython the type of your instance variable (e.g. v1) - Cython itself can happily access cdef attributes but it only knows about them if it knows the type. If it's just a variable then cdef that variable. If you're trying to use the return value from a function, or index a list, or similar, then you can use a cast: <Vectex>(func()).x. Only do this if you're certain of the type.

You can get similar errors with cdef functions, which are again only visible within Cython. In this case cpdef makes the function visible from to both Cython and Python. However, cpdef functions are in some ways the worst of all worlds (they have all the limitations of cdef functions and all the limitations of def functions) - you're usually better choosing an Cython-only (cdef) or Python (def) interface.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...