在Python中創建一個類時,我可以簡單地使def __len__(self):
方法使len(InstanceOfMyClass)
工作,但我無法找到如何做到這一點一個通過C-API的擴展類。Python的C-API使len(...)與擴展類一起工作
我嘗試添加一個__len__
方法,但似乎行不通
{"__len__",(PyCFunction)&TestClass_GetLen,METH_NOARGS,""},
的Python測試代碼:
def test(my_instance):
x = len(my_instance)#exception
return x
TypeError: object of type 'test_module.test_class' has no len()
代碼的TestClass
struct TestClass;
static int TestClass_Init(TestClass *self, PyObject *args, PyObject* kwds);
static void TestClass_Dealloc(TestClass *self);
static PyObject* TestClass_GetLen(TestClass *self);
struct TestClass
{
PyObject_HEAD;
};
static PyMethodDef TestClass_methods[] =
{
{"__len__",(PyCFunction)&TestClass_GetLen,METH_O,""},
{NULL}
};
static PyTypeObject TestClass_type = {PyObject_HEAD_INIT(NULL)};
bool InitTestClass(PyObject *module)
{
TestClass_type.tp_basicsize = sizeof(TestClass);
TestClass_type.tp_name = PY_MODULE_NAME".TestClass";
TestClass_type.tp_doc = "";
TestClass_type.tp_flags = Py_TPFLAGS_DEFAULT;
TestClass_type.tp_methods = TestClass_methods;
TestClass_type.tp_new = PyType_GenericNew;
TestClass_type.tp_init = (initproc)TestClass_Init;
TestClass_type.tp_dealloc = (destructor)TestClass_Dealloc;
if(PyType_Ready(TestClass_type) < 0) return false;
Py_INCREF(TestClass_type);
PyModule_AddObject(module, "TestClass", (PyObject*)&TestClass_type);
return true;
};
void TestClass_Dealloc(TestClass *self)
{
Py_TYPE(self)->tp_free((PyObject*)self);
}
int TestClass_Init(TestClass *self, PyObject *args, PyObject* kwds)
{
return 0;
}
PyObject* TestClass_GetLen(TestClass *self)
{
return PyLong_FromLong(55);
}
「類型錯誤:類型'test_module.test_class'的對象沒有len()」與「{」__len __「,(PyCFunction)&TestClass_GetLen,METH_O,」「}」:( – 2010-01-14 14:50:42
可以粘貼完整的C代碼嗎? – 2010-01-14 16:01:19
好吧,我添加了TestClass的所有代碼 – 2010-01-16 11:06:28