Search code examples
c++pythoncextend

@staticmethod when extending with c(++)


To create a class usable in Python is pretty straight-forward: http://code.activestate.com/recipes/54352-defining-python-class-methods-in-c/

But how to make methods static?


Solution

  • Use the METH_STATIC flag in PyMethodDef. The method will be passed NULL as the first parameter rather than an instance of the type.

    static PyMethodDef FooMethods[] = 
    {
        {"__init__", Foo_init, METH_VARARGS, 
         "doc string"},
        {"doSomething", Foo_doSomething, METH_VARARGS | METH_STATIC,
         "doc string"},
        {NULL},
    };