1

I am working on wrapping a C library using cython. I would like to use a cdef class to wrap a simple struct. The struct does require some parameters to be properly initialized. I put the corresponding code int the __cinit__ method like so:

cdef class Func:
    cdef library.Func* func

def __cinit__(self, int value):
    library.init_func(&self.func, value)

In python code I can create a function object via f = Func(17). The code then handles the initialization just fine. My question is the following: Say I want to extend the Func class in python (class MyFunc(Func): ...). How do I add a constructor to to MyFunc?

I would like to write a constructor with other parameters which calls __cinit__(self, int value) with a value derived from constructor parameters. But it does not seem to be possible (likely for good reason) to call __cinit__ from python code.

Is there some way to include constructors in subclasses or should I create a wrapper around the function and delegate methods / properties?

hfhc2
  • 4,182
  • 2
  • 27
  • 56
  • 1
    https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html#initialisation-methods-cinit-and-init says "If you need to pass a modified argument list to the base type, you will have to do the relevant part of the initialisation in the __init__() method instead (where the normal rules for calling inherited methods apply)." – ead Jan 11 '19 at 12:28

0 Answers0