Cython class AttributeError

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.

Leave a Comment