Inherit multiple cdef (mixin) classes

90 views
Skip to first unread message

jbrock...@gmail.com

unread,
Apr 4, 2021, 1:46:13 PM4/4/21
to cython-users
I have a python class that needs to subclass each of two cdef classes, which in general is not allowed.  The hope: if one of them is a mixin that doesn't implement __init__/__cinit__ or any cdef attributes, can this be worked around?

da-woods

unread,
Apr 4, 2021, 4:02:25 PM4/4/21
to cython...@googlegroups.com

My understanding is that it does work if the memory layouts are compatible (although I'm personally a little vague on what "compatible" means)

Certainly a genuinely empty cdef class + a "regular" cdef class does work:

cdef class C:
    cdef int a
    cdef float b

cdef class D:
    pass

then in Python I can successfully do:

class E(C, D):
   ...


However, if I add a cdef method to D then that creates a vtable pointer and you can no longer do this inheritance. Adding a def function doesn't break it though.

__init__ isn't a problem provided that you use super().

__cinit__ doesn't prevent you from subclassing it, although doesn't appear to get called for one of the classes (the rules about which one isn't obvious to me). Thus it's pretty pretty useless...

So it may work (under limited circumstances), but you may find it limited to the point of uselessness

David



On 04/04/2021 18:46, jbrock...@gmail.com wrote:
I have a python class that needs to subclass each of two cdef classes, which in general is not allowed.  The hope: if one of them is a mixin that doesn't implement __init__/__cinit__ or any cdef attributes, can this be worked around?

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/b4823c31-e95e-4565-8842-a1759c4511bbn%40googlegroups.com.


Brock Mendel

unread,
Apr 4, 2021, 5:09:59 PM4/4/21
to cython...@googlegroups.com
That's very helpful, thank you.

You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/HIAH6iQNWkw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/14173c5a-ba86-dc88-1d0a-8ce47034bf6c%40d-woods.co.uk.
Reply all
Reply to author
Forward
0 new messages