使用C语言扩展Python(二)
在上一篇中我们已经使用c语言实现了一个最简单的扩展模块,这一篇中将在其基础上进行功能的丰富。首先来考虑如何从外部的Python向C模块传递进参数,foo_bar2展示了如何向C模块传递整数,浮点数,字符串三个参数,其中"ids"指明了传入参数的数据类型。PyArg_ParseTuple负责对args进行解析,若解析失败则返回0.代码#include <Python.h>
static PyObject* foo_bar(PyObject* self, PyObject* args) {
Py_RETURN_NONE;
}
static PyObject* foo_bar2(PyObject* self, PyObject* args) {
int iNum;
double fNum;
char* str;
if (!PyArg_ParseTuple(args, "ids", &iNum, &fNum, &str)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyMethodDef foo_methods[] = {
{"bar",(PyCFunction)foo_bar,METH_NOARGS,NULL},
{"bar2", (PyCFunction)foo_bar2,METH_VARARGS,NULL},
{NULL,NULL,0,NULL}
};
PyMODINIT_FUNC initfoo() {
Py_InitModule3("foo", foo_methods, "My first extension module.");
} 你还可以指定可选的参数,只需要通过在格式字符串中包含一个"|"字符即可,如下所示:代码static PyObject* foo_bar2(PyObject* self, PyObject* args) {
int iNum;
double fNum;
char* str;
int iNum2 = 4;
double fNum2 = 5.0;
char *str2 = "hello";
if (!PyArg_ParseTuple(args, "ids|ids", &iNum, &fNum, &str,&iNum2, &fNum2, &str2)) {
相关文档:
#coding:utf-
8
__author__="sdm"
__author_email='sdmzhu3@gmail.com'
__date__ ="$2009-8-25 21:04:13$"
''
'
pytpl 类似
php的模板类
&nbs ......
/*****************test.c****************/
#include <stdio.h>
#include <stdlib.h>
#include "addr.h"
int main()
{
int flag=1;
while(flag)
{
switch(choose_menu())
{
case 1:add_person();break;
case 2:show_person_in ......
在编程的过程中,文件的操作是一个经常用到的问题,在C++Builder中,可以使用多种方法对文件操作,下面我就按以下几个部分对此作详细介绍,就是:
1、基于C的文件操作;
2、基于C++的文件操作;
3、基于WINAPI的文件操作;
4、基于BCB库的文件操作;
5、特殊文件的操作。
壹、基于C的文件操作
在ANSI C中, ......
经过几次面试,发现笔试题基本上都是那几道,没有什么创新或者改变,总结出来给大家参考参考.
一、请填写BOOL , float, 指针变量与“零值”比较的 if 语句。(10分)
请写出 BOOL flag 与“零值”比较的 if 语句。(3分)
标准答案:
if ( flag )
& ......
#include<windows.h>
void Birthday();
int main()
{
Birthday();
return 0;
}
void Birthday()
{
unsigned frequency[]={392,392,440,392,523,494,
392,392,440,392,587,523,
......