0

A Bar is associated with 0..6 Foos. Foo is an extension type, like so

cdef class Foo:
    cdef double fooiness

    cdef void do_things(self):
        self.fooiness += 1

I make copies of Bars very, very, very, very often. Ideally, I'd even like to be able to make copies of Bars concurrently with threads, without the GIL. I can define Bar like this:

cdef class Bar:
    cdef list my_foos

    cdef Bar copy(self):
        cdef Bar the_copy = Bar.__new__(Bar)
        the_copy.my_foos[:] = self.my_foos[:]
        return the_copy

but as you can imagine, the list[:] = list[:] bit shows up bright yellow in the -a report.

I've tried cdef Foo my_foos[6], but that gives me Array element cannot be a Python object

So I tried cdef Foo * my_foos[6], but that gave me Pointer base type cannot be a Python object

I've considered abandoning the array entirely and going with cdef Foo my_foo_1, my_foo_2... but I'd like to iterate over my Foos without having to hand-unroll the loop.

Searching variations on "how do I make an array of extension types in Cython" has turned up nothing relevant - which seems strange, since having arrays of things that aren't primitives is something you'd think a lot of people would want to do. I'm probably missing something obvious.

How do I make this work?

Schilcote
  • 2,344
  • 1
  • 17
  • 35
  • Duplicate of [cython: how do you create an array of cdef class](https://stackoverflow.com/questions/33851333/cython-how-do-you-create-an-array-of-cdef-class) – DavidW Dec 03 '19 at 08:27
  • Essentially the answer is "you can't really do this, and if you could then it would end up pretty close to a Python list, so just use a Python list". I'd probably change `the_copy.my_foos[:] = self.my_foos[:]` to `the_copy.my_foos = list(self.my_foos)` to make a copy but it'll be pretty similar performance-wise – DavidW Dec 03 '19 at 08:28

0 Answers0