How would one go about specifying a static method or variable in a python class, in CPython C++?
In the PyTypeObject structure, tp_getset, tp_methods, tp_members all seem to assume an instance of the class.
Thanks very much, Rob.
How would one go about specifying a static method or variable in a python class, in CPython C++?
In the PyTypeObject structure, tp_getset, tp_methods, tp_members all seem to assume an instance of the class.
Thanks very much, Rob.
Static and class methods can be defined in tp_methods by adding METH_STATIC or METH_CLASS to the ml_flags field of the PyMethodDef structure. This is equivalent to @staticmethod and @classmethod decorators.
The first parameter of the method, which normally is the instance pointer, will be NULL for static methods and PyTypeObject* for class methods.
http://docs.python.org/c-api/structures.html#PyMethodDef
Class attributes can be added by setting the tp_dict to a dictionary with these attributes before calling PyType_Ready() (in your module initialization function). Alternatively tp_dict can be left as NULL in which case PyType_Ready() will create the dict for you. The attributes can then be added to that dict.
http://docs.python.org/c-api/typeobj.html#tp_dict
Computed class attributes require a descriptor class, exactly as in Python. An instance of the descriptor should then be added to the tp_dict as with other class attributes.