2006年03月31日 星期五 11:37
有人用过在c环境中通过PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)调用类的成员函数吗? 我有个简单的类tank.py class tank: def __init__(self,x,y): tank.x=x tank.y=y def forward(self): print 'forward' c环境下程序如下: #include "python.h" int main(int argc,char ** argv) { PyObject *TankObject, *Tank_dict, * expression; Py_Initialize(); * TankObject* = PyImport_AddModule("tank"); //这个TankObject是什么?tank类的对象吗?还是文件对象?我该怎么获得tank类的对象? //是不是需要声明一个Python对象?? Tank_dict = PyModule_GetDict(TankObject); expression = PyDict_GetItemString(Tank_dict, "forward"); PyObject_CallObject(expression, NULL); //PyObject_CallMethod(TankObject,"forward",NULL); Py_Finalize(); return 0; } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060331/51ac4c85/attachment.htm
2006年03月31日 星期五 11:53
具体没做过,但根据我对Python源码的理解,有一些可以提供你参考: *TankObject* = PyImport_AddModule("tank"); 用PyImport_AddModule只是创建了一个名为"tank"的module,并没有进行import动作,建议使用PyImport_ImportModule("tank"),这个会进行import动作 这个TankObject是一个PyModuleObject对象,对于这个对象,你可以这样理解它: struct PyModuleObject { PyDictObject* dict; } 而对于PyDictObject对象,你可以这样理解: struct PyDictObject { mapdict; } 所以import之后内存中的结构如下: ["tank", PyModuleObject]---->["tank", class object]---->["forward", function object]。 所以你的调用应该是这样的: Tank_dict = PyModule_GetDict(TankObject); class_object = PyDict_GetItemString(Tank_dict, "tank"); function_object = PyObject_GetAttrString(class_object, "forward") PyObject_CallObject(function_object, NULL); 呵呵,没有测试过,你试试看吧 :) On 3/31/06, 吴俊玉 <wujunyu at gmail.com> wrote: > > 有人用过在c环境中通过PyObject* PyObject_CallMethod(PyObject *o, char *method, > char *format, ...)调用类的成员函数吗? > 我有个简单的类tank.py > class tank: > def __init__(self,x,y): > tank.x=x > tank.y=y > > def forward(self): > print 'forward' > > c环境下程序如下: > > #include "python.h" > > int main(int argc,char ** argv) > { > PyObject *TankObject, *Tank_dict, * expression; > Py_Initialize(); > * TankObject* = > PyImport_AddModule("tank"); > > > //这个TankObject是什么?tank类的对象吗?还是文件对象?我该怎么获得tank类的对象? > //是不是需要声明一个Python对象?? > Tank_dict = PyModule_GetDict(TankObject); > expression = PyDict_GetItemString(Tank_dict, "forward"); > PyObject_CallObject(expression, NULL); > //PyObject_CallMethod(TankObject,"forward",NULL); > Py_Finalize(); > return 0; > } > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- Robert Python源码剖析――http://blog.donews.com/lemur/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060331/610fef8c/attachment.htm
2006年03月31日 星期五 13:15
豁然开朗,至少对这一块理解有点清晰了 谢谢. 代码如下,不过仍然运行错误! int main(int argc,char ** argv) { Py_Initialize(); PyObject *TankObject, *Tank_dict, *class_object,* function_object; TankObject = PyImport_ImportModule("MyTank"); Tank_dict = PyModule_GetDict(TankObject); class_object = PyDict_GetItemString(Tank_dict, "tank"); function_object = PyObject_GetAttrString(class_object, "forward"); PyObject_CallMethod(class_object,"forward",NULL); Py_Finalize(); return 0; } 我跟进去调试发现 TankObject = PyImport_ImportModule("MyTank"); 数据窗口显示: TankObject CXX0017: Error: symbol "TankObject" not found -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060331/397535a2/attachment.html
2006年03月31日 星期五 15:59
下面这个经过我测试了,可以运行: int main() { PyObject *TankModule = NULL; PyObject *TankDict = NULL; PyObject *TankClassObj = NULL; PyObject *ForwardFuncObj = NULL; PyObject *TankObject = NULL; Py_Initialize(); //import module TankModule = PyImport_ImportModule("MyTank"); //获得module中的维护的dict TankDict = PyModule_GetDict(TankModule); //从dict中获得symbol "Tank" 对应的对象,是一个PyClassObject TankClassObj = PyDict_GetItemString(TankDict, "Tank"); //PyClassObject是callable的,创建PyClassObject对象的实例,这里即是OO中的class的实例化。TankObject是一个PyInstanceObject TankObject = PyObject_Call(TankClassObj, NULL, NULL); //这个不用comment了吧 :) PyObject_CallMethod(TankObject, "forward", NULL); Py_Finalize(); return 0; } On 3/31/06, 吴俊玉 <wujunyu at gmail.com> wrote: > > 豁然开朗,至少对这一块理解有点清晰了 > 谢谢. > 代码如下,不过仍然运行错误! > int main(int argc,char ** argv) > { > Py_Initialize(); > PyObject *TankObject, *Tank_dict, *class_object,* function_object; > TankObject = PyImport_ImportModule("MyTank"); > > Tank_dict = PyModule_GetDict(TankObject); > class_object = PyDict_GetItemString(Tank_dict, "tank"); > function_object = PyObject_GetAttrString(class_object, "forward"); > PyObject_CallMethod(class_object,"forward",NULL); > Py_Finalize(); > return 0; > } > 我跟进去调试发现 > TankObject = PyImport_ImportModule("MyTank"); > 数据窗口显示: > TankObject CXX0017: Error: symbol "TankObject" not found > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- Robert Python源码剖析――http://blog.donews.com/lemur/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060331/ce395dc5/attachment.html
2006年03月31日 星期五 16:58
代码已收到 非常感谢 先消化下,再不懂的时候再麻烦你了! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060331/62f101fd/attachment.html
Zeuux © 2025
京ICP备05028076号