MiKL
MiKL

Reputation: 73

Cython/python - List of cdef class

I am trying to improve this code which uses list type :

# CLASS ORDERC #
################
cdef class OrderC:
    cdef int _side
    cdef float _px
    cdef int _vo

    def __cinit__(self, int side, float px, int vo):
        # ....

    cdef setData(self, double[:] dates):
        # ....

# CLASS LISTORDERC #
####################
cdef class ListOrderC:
    cdef int _B
    cdef list _LO_Bid

    cdef double[:] _dates

    def __init__(self, num.ndarray[num.double_t, ndim=1] dates):
        self._B = 0
        self._LO_Bid = []
        self._dates = dates

    cpdef addOrder(self, OrderC oo):
       self._B += 1
       self._LO_Bid.append(oo)
       self._LO_Bid[-1].setData(self._dates)

The problem arrives when i call addOrder from python :

 AttributeError: 'OrderC.OrderC' object has no attribute 'setData'

I guess it's because OrderC is recognized as python object, so I have to define setData with cpdef. But I want OrderC to be recognized as the cdef class to imporve the performance.

Could you please help me ?

Thanks

Upvotes: 1

Views: 2000

Answers (1)

Veedrac
Veedrac

Reputation: 60137

The problem is that list only holds things of type object, so when you access you get back an object.

You probably want to use a C++ vector instead.

Upvotes: 1

Related Questions