[Cython] non-virtual methods

Vitja Makarov vitja.makarov at gmail.com
Wed Aug 24 21:17:52 CEST 2011


2011/8/24 Stefan Behnel <stefan_ml at behnel.de>:
> Vitja Makarov, 24.08.2011 21:00:
>>
>> Recently I used cython in my project and I came with idea that
>> sometimes virtual methods are overkill.
>>
>> What's about adding non-virtual decorator for c[p]def methods?
>>
>> cdef class Foo:
>>      @cython.nonvirtual
>>      cpdef int is_active(self):
>>              return clib.obj_is_active(self._cobj)
>
> How does that differ from the "final" decorator?
>

I tried final classes:
1. Final decorator doesn't work at pxd level (compiler crash)
2. In this example foo call is done through virtual table

cimport cython

@cython.final
cdef class Foo:
    cdef foo(self):
        print 'haha'

def test():
    cdef Foo a = Foo()
    a.foo()

  __pyx_t_1 = ((struct __pyx_vtabstruct_3yyy_Foo
*)__pyx_v_a->__pyx_vtab)->foo(__pyx_v_a); if (unlikely(!__pyx_t_1))
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno =
__LINE__; goto __pyx_L1_error;}

3. I can't use final decorator for methods (error reported)


-- 
vitja.


More information about the cython-devel mailing list