Hello, I would like to have a "virtual" cpdef method: something which allows a fast Cython call *when implemented* but without a default implementation. The most simple implementation would be a "cpdef" method but without an entry for the method in the Python method table. Of course, the "cdef" part needs to be implemented by the user (you need to put something in the vtab). For my purposes, something like "raise AttributeError" would be sufficient. 1. Can this be accomplished currently in Cython? 2. Is this something that makes sense to add to Cython? 3. What do you think of the following syntax? cdef class MyClass(object): cpdef virtual_method(self): # implementation of "cdef" part ... del virtual_method # don't put virtual_method in the method table
All methods (cdef, cpdef, and def) are virtual by default in Cython, just like Python. Sounds like you want a cdef function to me. You can override a cdef function with a cpdef function for any subclass that wishing to expose it to Python. On Mon, Jun 20, 2016 at 6:08 AM, Jeroen Demeyer <jdemeyer@cage.ugent.be> wrote:
Hello,
I would like to have a "virtual" cpdef method: something which allows a fast Cython call *when implemented* but without a default implementation.
The most simple implementation would be a "cpdef" method but without an entry for the method in the Python method table.
Of course, the "cdef" part needs to be implemented by the user (you need to put something in the vtab). For my purposes, something like "raise AttributeError" would be sufficient.
1. Can this be accomplished currently in Cython?
2. Is this something that makes sense to add to Cython?
3. What do you think of the following syntax?
cdef class MyClass(object): cpdef virtual_method(self): # implementation of "cdef" part ...
del virtual_method # don't put virtual_method in the method table _______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel
Robert Bradshaw wrote:
All methods (cdef, cpdef, and def) are virtual by default in Cython, just like Python.
On Mon, Jun 20, 2016 at 6:08 AM, Jeroen Demeyer <jdemeyer@cage.ugent.be> wrote:
I would like to have a "virtual" cpdef method: something which allows a fast Cython call *when implemented* but without a default implementation.
It sounds like Jeroen really means "abstract" here, not "virtual". The usual way to do this kind of thing in Python is to write a stub method that raises NotImplementedError or such like. -- Greg
On 2016-06-20 17:31, Robert Bradshaw wrote:
All methods (cdef, cpdef, and def) are virtual by default in Cython, just like Python.
I meant "pure virtual" or "abstract": just declared but without an implementation.
Sounds like you want a cdef function to me. You can override a cdef function with a cpdef function for any subclass that wishing to expose it to Python.
I was totally not aware that you could override cdef with cpdef. But hang on, are you sure that this works? The C functions for cdef and cpdef functions have a different signature: cpdef functions have a __pyx_skip_dispatch argument while cdef functions don't have such an argument.
On 2016-06-21 08:17, Jeroen Demeyer wrote:
But hang on, are you sure that this works? The C functions for cdef and cpdef functions have a different signature: cpdef functions have a __pyx_skip_dispatch argument while cdef functions don't have such an argument.
Answering my own question, it seems that Cython uses different vtab entries for the cdef and cpdef functions, where the cdef one calls the cpdef one with __pyx_skip_dispatch=0. The only difference with my proposal is that the cdef method cannot automatically be overridden by a def method. But that can be solved by cdef class MyClass(object): cdef foo(self): return (<object>self).foo() So this overriding of cdef methods by cpdef methods might completely solve my use case. Thanks!
participants (3)
-
Greg Ewing -
Jeroen Demeyer -
Robert Bradshaw