[Cython] Python callable staticmethods?

Robert Bradshaw robertwb at gmail.com
Mon Sep 25 20:33:03 EDT 2017


On Mon, Sep 25, 2017 at 12:58 PM, John Ehresman <jpe at wingware.com> wrote:
> On 9/25/17 3:32 PM, Stefan Behnel wrote:
>>
>> Am 25. September 2017 20:59:01 MESZ schrieb John Ehresman:
>>>
>>> Is there a way to define a Python callable staticmethod in a Cython
>>> cdef class?
>>
>>
>> Did you try it?
>
>
> Not fully, I'll admit.  I found that the following are invalid in a .pxd:
>
> cdef class Cls:
>
>   @staticmethod
>   def method(a)
>
>   @staticmethod
>   cpdef method(a)
>
> I later found that just using @staticmethod\n def method(a) in a .py file
> does work (I'm using pure mode).  That means that cython code in other
> modules is doing a PyObject_GetAttr to get the method and then calling the
> returned object (admittedly I'm not far enough along to profile to see if
> this is a real problem or not).
>
> I think I'm really asking about cpdef staticmethods.

You can write cdef static methods and "def" static methods, but cpdef
is not yet supported.


More information about the cython-devel mailing list