Skip to content Skip to sidebar Skip to footer

Can A Cdef Class Store A Variable That Isn’t (type-)declared?

I’m curious if the following is valid, where only some of the variables are type-declared in a type-declared class. That is, would cdef before the class be invalid in this case?

Solution 1:

Short answer:

No, you need to declare it. Otherwise, you'll get an AttributeError: 'xxx.CythonClass' object has no attribute 'defaultdict' error.

(slightly) longer answer:

You can always declare it as (python) object:

cdef classCythonClass(object):

    cdef int var1, var2
    cdef object defaultdict  # declared as python object

This won't be very efficient, but it works.

Post a Comment for "Can A Cdef Class Store A Variable That Isn’t (type-)declared?"