Implementing a "classmethod" in an extension type

Mark Franklin mfranklin at
Tue Apr 1 15:59:16 CEST 2003


I've got an built-in extension type, implemented in C. I want to be able to implement the equivalent of a "classmethod" in that type. 

I thought I might be able to do something like this, which fairly closely mimics what you do in Python:

	PyTypeObject myType = {
		// initialised in the usual way. It's tp_methods field includes a method definition for "MyMethod".......

	PyObject *method = PyObject_GetAttrString(myType, "MyMethod");
	// method is a PyMethodDescrObject
	PyObject *classMethod = PyClassMethod_New(method);
	PyObject_SetAttrString(myType, "MyMethod", classMethod);


However, when I do this I get a TypeError exception from the PyObject_SetAttrString call: "can't set attributes of built-in/extension type 'MyType' ".

So, how do I get the class method into the type object?


More information about the Python-list mailing list