Python嵌入C++详解(3)
继前篇《Import Module》(http://blog.csdn.net/xiadasong007/archive/2009/09/02/4512797.aspx),继续分析嵌入部分基础知识。这次不多说,有什么问题记得多查英文资料,国内的这方面知识少
还是来看代码,写完我就睡觉了~
#include "python/python.h"
#include <iostream>
using namespace std;
int main(int argc,char** argv)
{
PyObject *pModule, *pClass,*pStuObj,*pFunc;
Py_Initialize();
////////////////
//载入模块stu.py
pModule = PyImport_ImportModule ("stu");
//从模块中得到类Student
pClass = PyObject_GetAttrString (pModule, "Student");
//生成一个学生对象jack,注意3.1版本必须加上小括号:(s),否则会出错
//PyObject_CallObject和另外一个常用的PyEval_CallObject用法极为相近,请看附录部分详解
PyObject * temp=Py_BuildValue("(s)","jack");
pStuObj = PyObject_CallObject(pClass, temp);
//获得函数对象jack中的成员函数printName,
//这个getattr的意思就是得到当前对象的属性(不要局限于C++中的属性哦)
pFunc= PyObject_GetAttrString (pStuObj, "printName");
//调用函数printName
PyEval_CallObject (pFunc, NULL);
////////////////
Py_Finalize();
return 0;
}
/*
附录:
1:Student类,我发现print后面现在必须加上小括号了,否则会有问题
class Student:
name=""
def __init__(self,name):
self.name=name
def printName(self):
print (self.name)
2:PyObject_CallObject和 PyEval_CallObject不同之处:
可以看出,后者较为直接,而且接受NULL,and does explicit type checks
for args and kwds.
PyObject_CallObject(PyObject *o, PyObject *a)
{
! PyObject *r;
! PyObject *args = a;
!
! if (args == NULL) {
! args = PyTuple_New(0);
! if (args == NULL)
! return NULL;
! }
!
! r = PyEval_CallObject(o, args);
!
相关文档:
#include <stdio.h>
#include <windows.h>
#include <mysql.h>
#define host "localhost"
#define username "root"
#define password "123"
#define database "oa"
MYSQL *conn;
int main()
{
MYSQL_RES *res_set;
MYSQL_ROW row;
unsigned int i,ret;
FILE *fp;
MYSQL_FIELD *field;
......
filename=raw_input('enter file name:')
f=open(filename,'rb')
f.seek(0,0)
index=0
for i in range(0,16):
print "%3s" % hex(i) ,
print
for i in range(0,16):
print "%-3s" % "#" ,
print
while True:
temp=f.read(1)
if len(temp) == 0:
break
else:
print "%3s" % temp.encode('hex'),
......
今天做ftp的界面,做的相当郁闷,弄得心情及其不爽,在网上搜到死都不知道该怎么办,打算明天先看看C++
的是怎么弄的再说。不过,现在我想写一下关于socket的编程。
先写一个时间服务器吧,他监听端口,并且会返回 服务器的时间
server.py
#!/usr/bin/python
# Copyright (c) angelipin (angelipin@126.com)
import ......
1. 事件驱动
一个事件及其回调的例子是鼠标移动。我们假设鼠标指针停在您GUI 程序的某处。如果鼠标被移到了程序的别处,一定是有什么东西引起了屏幕上指针的移动,从而表现这种位置的转移。系统必须处理这些鼠标移动事件才能展现(并实现)鼠标在窗口上的移动。一旦您释放了鼠标,就不再会有事件需要处 ......