大一python调用函数 python入门之函数调用( 二 )


example.fact(4)
3. Python 调用 C++ (Boost.Python)
Boost库是非常强大的库, 其中的python库可以用来封装c++被python调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.
首先在ubuntu下安装boost.python, apt-get install libboost-python-dev
#include boost/python.hpp
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
把代码存为hello.cpp, 编译成so库
g++ hello.cpp -o hello.so -shared -I/usr/include/python2.5 -I/usr/lib/python2.5/config -lboost_python-gcc42-mt-1_34_1
此处python路径设为大一python调用函数你的python路径, 并且必须加-lboost_python-gcc42-mt-1_34_1, 这个库名不一定是这个, 去/user/lib查
然后在有此so库的目录, 进入python, 可以如下使用
import hello
hello.greet()
'hello, world'
4. python 调用 c++ (ctypes)
ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.
ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.
#include Python.h
class TestFact{
public:
TestFact(){};
~TestFact(){};
int fact(int n);
};
int TestFact::fact(int n)
{
if (n = 1)
return 1;
else
return n * (n - 1);
}
extern "C"
int fact(int n)
{
TestFact t;
return t.fact(n);
}
将代码存为wrapper.cpp不用写python接口封装, 直接编译成so库,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
进入python, 可以如下使用
import ctypes
pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')
pdll.fact(4)
12
python如何调用函数中的数组python调用函数中的数组的方法:
在函数里面使用global定义一个全局变量 , 然后将数组赋值给这个变量,调用该函数,带有数组的的这个全局变量就可以直接使用了
示例如下:
执行结果如下:
更多Python知识,请关注:Python自学网?。?
python类中如何自动调用函数?答: 你讲大一python调用函数的这个是pyqt里面大一python调用函数的内容,刚好大一python调用函数我最近也一直在学,在代码当中确实没有显式调用这个函数,但是你要知道,keyPressEvent是一个槽函数,在系统内部定义大一python调用函数了这个函数,但是里面没有任何代码,而你就是对它重写大一python调用函数了,就是说,只要你的键盘里面的任何一个键按下,就相当于会发送一个信号,那么在内部就会自动调用这个函数 。类似的函数还有 keyReleaseEvent(键位松开时自动触发)等 。希望可以帮助到你 。
相关介绍
【大一python调用函数 python入门之函数调用】大一python调用函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python入门之函数调用、大一python调用函数的信息别忘了在本站进行查找喔 。

推荐阅读