python的C、c++扩展
python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 TestCLib.c: 提供python的模块接口
#include "Python.h"
#include <stdlib.h>
#include <stdio.h>
long fac(long);
// ------------------------------------------------------
// Make C code usable in Python
// ------------------------------------------------------
PyObject* TestCLib_fac(PyObject * self, PyObject *args)
{
long num;
if ( ! PyArg_ParseTuple(args,"l",&num)){
return NULL;
}
return (PyObject*)Py_BuildValue("l",fac(num));
}
static PyMethodDef TestCLibMethods[] = {
{"fac",TestCLib_fac,METH_VARARGS},
{NULL,NULL}
};
void initTestCLib()
{
Py_InitModule("TestCLib",TestCLibMethods);
}
1.2 test.c: 具体的C语言实现
#include <stdlib.h>
#include <stdio.h>
// ------------------------------------------------------
// Pure C code
// ------------------------------------------------------
long fac(long n)
{
if ( n < 0){
return fac(-n);
} else if ( n < 2 ){
return 1;
} else {
return n * fac(n-1);
}
}
1.3 test.py: 测试脚本
#!/usr/bin/env python
import TestCLib as TestLib
for i in range(10,20) :
f1 = TestLib.fac(i)
print "%d! = %d"%(i,f1)
1.4 编译与运行
编译命令:
gcc -fPIC -shared -o TestCLib.so TestCLib.c test.c -I /usr/local/python/include/python2.6/
将生成的动态链接库 T
相关文档:
原帖一:http://blog.csdn.net/dotphoenix/archive/2009/05/20/4203075.aspx
原贴二:http://www.cocoachina.com/bbs/read.php?tid-8008.html
@property (参数) 类型 名字;
这里的参数主要分为三类:读写属性(readwrite/readonly),setter语意(assign/retain/copy)以及atomicity(nonatomic)。
assign/retain/copy ......
一个控制台下的数字表达式求值程序 (c/c++)
源代码见下:
#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stack>
using namespace std;
//设置运算符优先级的算法
int Priority(const string opera) // 运算符 ......
1 编程基础
1.1 基本概念
1. 的理解:const char*, char const*, char*const的区别问题几乎是C++面试中每次 都会有的题目。 事实上这个概念谁都有只是三种声明方式非常相似很容易记混。 Bja ......
版权申明:以下内容属于作者正在写作的《软件测试实践》一书的内容,未经许可不得用于任何正式出版物中,如果转载请注明出处。
作者:周伟明
代码检视要点
代码检视技能属于开发人员的基本功,能够很大程度地反应出开发人员的能力水平,前面4.4.1节已经讲过提高评审检视的方法。下面以实际的C/C++语言方面的代码来讲解 ......
1/有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
==============================================================
2/企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高
于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提
......