[]:Python怎么访问C++自定义的结构(类)的对象
发布时间:2011-06-29 20:03:51 文章来源:www.iduyao.cn 采编人员:星星草
[求助]:Python如何访问C++自定义的结构(类)的对象
使用的是Boost.Python库,在程序中有一个正在运行的自定义结构(类)的对象,希望传入到Python解释器中进行运算。不知道这个功能怎么实现,祈求高手相助!
------解决方案--------------------
感觉有必要为本版培养一位有C++和Python交互经验并热爱灌水的网友了……
------解决方案--------------------
不会C++,只会点Python
友情帮你UP了
------解决方案--------------------
为了能在Python中使用自定义的类型。
1 写一个Python扩展,把想在Python中操作的类注册到Python中。使用boost库这一步很方便。
简单的代码示例如下:
//operand.h
struct Operand{
Operand(int l,int r)
:lhs(l),rhs(r){
}
int lhs ;
int rhs ;
} ;
int Operator(Operand*) ;
//operand.cpp
#include "Operand.h "
#include <iostream>
int Operator(Operand* op){
std::cout < < "Operator(Operand*) " < <std::endl ;
return op-> lhs + op-> rhs ;
}
//main.cpp
#include "Operand.h "
#include <boost/python.hpp>
using namespace boost::python ;
BOOST_PYTHON_MODULE(Operand)
{
class_ <Operand> ( "Operand ",init <int,int> ())
.def_readwrite( "lhs ",&Operand::lhs)
.def_readwrite( "rhs ",&Operand::rhs)
;
def( "Operator ",Operator) ;
}
2 写一段Python代码。如下:
//test.py
from Operand import *
def add(Op):
print 'test.py '
print type(Op)
return Op.lhs+Op.rhs
3 写使用Python的程序,这一步也使用boost
//operand.h
struct Operand{
Operand(int l,int r)
:lhs(l),rhs(r){
}
int lhs ;
int rhs ;
} ;
//main.cpp
#include "Operand.h "
#include <boost/python.hpp>
using namespace boost::python ;
#include <iostream>
int main(){
Py_Initialize() ;
try{
object main_module((
handle <> (borrowed(PyImport_AddModule( "__main__ ")))));
object main_namespace = main_module.attr( "__dict__ ");
handle <> ignored((PyRun_String(
"from test import *\n "
, Py_file_input
, main_namespace.ptr()
, main_namespace.ptr())
));
object func=main_module.attr( "add ") ;
Operand oper(2,4) ;
object param(oper) ;
PyObject* pArgs = PyTuple_New( 1 ) ;
Py_INCREF( param.ptr() ) ;
PyTuple_SetItem( pArgs , 0 , param.ptr() ) ;
handle <> result( PyObject_CallObject(func.ptr(),pArgs ) ) ;
Py_DECREF( pArgs ) ;
int value=extract <int> ( object(result)) ;
std::cout < <value < <std::endl ;
}
catch( error_already_set ){
PyErr_Print() ;
}
Py_Finalize() ;
return 0 ;
}
还有一些引用计数的代码放在中间比较讨厌。boost对嵌入Python做的还不是很方便。
用Rational Purify了一下。没有内存泄漏。
有问题再讨论。一起学习前进。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
能不能找到支持 python 2.6 2.7 3.x 版本的 mod_python 呢?
http://archive.apache.org/dist/httpd/modpython/win/3.3.1/
我在...
-
windows下安装apache + python + django + mod_wsgi.so
对应版本:
Apache:Apache HTTP Server (httpd) 2.2.19
Python:Python2.7
Dja...
-
手工执行python3程序没问题,放在cron里面就不执行有中文的代码,高手帮忙啊。急!
先介绍下基本情况
环境: redhat Python3.2
目的:定...
-
PYTHON用什么编辑器?
是用自带的IDLE不?
------解决方案--------------------
看下国外的Python用户都用什么吧:
http://jobs.pyth...
-
为什么在python25中输入下面的代码是错误的??求大神!
if 1 < 0:
print '”x” must be atleast 0!'
------解决方案-----...
-
pyhthon zipfile获取压缩文件列表后怎样打开其中某个文件?
如题。似乎ZipFile没有open操作..
zCmfile = zipfile.ZipFile(target...
-
本人菜鸟一个,哪位大神帮我解释一下下面两段
import sys
print >> sys.stderr, 'Fatal error: invalid input!'
import sys
...
-
老王的python学习网站!推荐!
http://blog.csdn.net/hendom/article/details/7173207
很不错的python学习网站。
http://www.cnpyt...
-
myeclipse里导入python项目
初学python,我在myeclipse里导入已有项目,选择路径后为什么没出现该项目,这项目不应该有问题啊,我用的是m...
-
安装PyQt的问题?
今天在ubuntu下安装了PyQt-x11-gpl-4.9,但是我按照《getting started with PyQt》上的一个例子
import sys
from ...