ºÜºÃµÄc++ºÍPython»ìºÏ±à³ÌÎÄÕÂ
http://www.cppblog.com/jacky2019/archive/2007/05/17/24269.html
c++ÖÐǶÈëpythonÈëÃÅ1
±¾ÈËÊÇÓÃvc2003+python2.5ѧϰµÄ£¬ÆäËüµÄÒ²Ó¦¸Ã²î²»Á˶àÉÙ
0. »µ¾³ÉèÖÃ
°ÑpythonµÄinclude/libsĿ¼·Ö±ð¼Óµ½vcµÄinclude/lib directoriesÖÐÈ¥¡£ÁíÍ⣬ÓÉÓÚpythonûÓÐÌṩdebug lib£¬ÌåµØ˵£¬¾ÍÊÇûÓÐÌṩpython25_d.libÁË¡£Äã¿ÉÒÔ×Ô¼º±àÒëpythonµÄÔ´´úÂëÀ´µÃµ½python25_d.libµÄ£¬Å¼»¹Ã»ÊÔ¹ý£¬ºÇºÇ¡£¶øÇÒÍøÉÏÕÒÁËÒ»ÏÂҲûÏÂÔص½¡£ËùÒÔ£¬Èç¹ûÄãÏëÒªÔÚdebugÏÂÔËÐгÌÐòµÄ»°£¬ÄãÒª°Ñpyconfig.h£¨ÔÚpython25/include/Ŀ¼Ï£©µÄ´ó¸ÅÊÇÔÚ283ÐУ¬°Ñpragma comment(lib,"python25_d.lib")¸Ä³Épragma comment(lib,"python25.lib")£¬ÈÃpython¶¼Ê¹Ó÷Çdebug lib.
1. ¿ªÊ¼±à³ÌÁË
#include <python.h>
µÚÒ»²½¾ÍÊÇ°üº¬pythonµÄÍ·Îļþ
2. ¿´Ò»¸öºÜ¼òµ¥µÄÀý×Ó
1)pythonÎļþtest.py£¬ºÜ¼òµ¥µÄ¶¨ÒåÁËÒ»¸öº¯Êý
#Filename test.py
def Hello():
print "Hello, world!"
Õâ¸öÓ¦¸ÃÄÜ¿´¶®µÄ°É£¿·ñÔòµÄ»°£¬»ØÈ¥ÔÙÁ·Á·python°É£¬ºÇºÇ¡£¡¶¼òÃ÷Python½Ì³Ì¡·Swaroop, C. H. Öø¡£Éò½àÔª Òë¡£
2)cppÎļþ
#include <python.h> //°üº¬Í·Îļþ£¬ÔÚc++ÖÐǶÈëpython£¬ÕâÊDZØÐëµÄ
int main()
{
Py_Initialize();
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
pModule = PyImport_ImportModule("test");
pFunc = PyObject_GetAttrString(pModule, "Hello");
PyEval_CallObject(pFunc, NULL);
Py_Finalize();
return 0;
}
µÚÒ»²½»¹ÊÇ°üº¬Í·Îļþ
µÚ¶þ²½£¬Ê¹ÓÃpython֮ǰ£¬Òªµ÷ÓÃPy_Initialize();Õâ¸öº¯Êý½øÐгõʼ»¯¡£
°ïÖúÎĵµÖÐÈçÊÇ˵£º
The basic initialization function is Py_Initialize(). This initializes the table of loaded modules, and creates the fundamental modules __builtin__, __main__, sys, and exceptions. It also initializes the module search path (sys.path).
·´Õý£¬Ò»¿ªÊ¼ÄãÒ»¶¨Òªµ÷Óá£
µÚÈý²½£¬ÉùÃ÷һЩPythonµÄ±äÁ¿£¬PyObjectÀàÐ͵ġ£ÆäʵÉùÃ÷Ò²¿É·ÅÔÚÇ°Ã棬Õâ¸öµ¹ÊÇÎÞËùνµÄ¡£
µÚËIJ½£¬import module£¬Ò²¾ÍÊÇÄãµÄ½Å±¾Ãû×Ö£¬²»ÐèÒª¼Óºó׺Ãû£¬·ñÔò»á³ö´íµÄ¡£
µÚÎå²½£¬´ÓÄãimport½øÀ´µÄmoduleÖеõ½ÄãÒªµÄº¯Êý
pFunc = PyObject_GetAttrString(pModule, "Hello");
ÉÏÃæµÄÀý×ÓÒѾ¹»Çå³þ
Ïà¹ØÎĵµ£º
client:
import socket, sys
if __name__ == '__main__':
#´¦Àí²ÎÊý
argv = sys.argv
if (len(argv)!=3) or (len(argv)==2 and argv[1]=='/?'):
print '>>>Useage:', argv[0], '<address> < ......
PythonµÄÃæÏò¶ÔÏóÐÔÖÊ
ÀàºÍ¶ÔÏóÊÇÃæÏò¶ÔÏó±à³ÌµÄÁ½¸öÖ÷Òª·½Ãæ¡£Àà´´½¨Ò»¸öÐÂÀàÐÍ£¬¶ø¶ÔÏóÕâ¸öÀàµÄ ʵÀý ¡£ÕâÀàËÆÓÚÄãÓÐÒ»¸öintÀàÐ͵ıäÁ¿£¬Õâ´æ´¢ÕûÊýµÄ±äÁ¿ÊÇintÀàµÄʵÀý£¨¶ÔÏ󣩡£
ÐèҪעÒâµÄÊÇ£¬ÔÚPythonÖУ¬¼´±ãÊÇÕûÊýÒ²±»×÷Ϊ¶ÔÏó£¨ÊôÓÚintÀࣩ¡£ÕâºÍC++¡¢Java£¨1.5°æ֮ǰ£©°ÑÕûÊý´¿´â×÷ΪÀàÐÍÊDz»Í¬µÄ¡£Í¨¹ ......
PythonÖеÄÎļþ²Ù×÷ÒÔ¼°ÊäÈëÊä³ö
ÎÒÃÇ¿ÉÒÔ·Ö±ðʹÓÃraw_inputºÍprintÓï¾äÀ´Íê³ÉÕâЩ¹¦ÄÜ¡£¶ÔÓÚÊä³ö£¬ÄãÒ²¿ÉÒÔʹÓöàÖÖ¶àÑùµÄstr£¨×Ö·û´®£©Àà¡£ÀýÈ磬ÄãÄܹ»Ê¹ÓÃrjust·½·¨À´µÃµ½Ò»¸ö°´Ò»¶¨¿í¶ÈÓÒ¶ÔÆëµÄ×Ö·û´®¡£ÀûÓÃhelp(str)»ñµÃ¸ü¶àÏêÇé¡£
ÁíÒ»¸ö³£ÓõÄÊäÈë/Êä³öÀàÐÍÊÇ´¦ÀíÎļþ¡£´´½¨¡¢¶ÁºÍдÎļþµÄÄÜÁ¦ÊÇ ......
¼ÆËãÏß³ÌÖ´ÐÐijÏîÈÎÎñÏûºÄµÄʱ¼äʱ£¬Ðí¶à¿ª·¢ÈËÔ±»áµ÷ÓÃGetTickCount/GetTickCount64±àдÈçϵĴúÂ룺
// Get the current time (start time)
ULONGLONG qwStartTime = GetTickCount64();
// Perform complex algorithm here
// Subtract start time from current time to get duration
ULONGLONG dwElapsedTime = Get ......
3¡¢Lists Êý¾ÝÀàÐÍ
ÔÚpythonÖУ¬listsÊÇÐòÁÐÈÝÆ÷£¬Ëü¿ÉÒÔÈÝÄÉÈκÎÀàÐ͵ĶÔÏ󡣺Ístrings²»Í¬£¬ListsÖ§³ÖÔµØÐÞ¸Ä(mutalbe in place),ͨ¹ýϱêÒýÓõķ½Ê½£¬¿ÉÒÔÐÞ¸ÄListsµÄÄÚÈÝ£¬ÈçpÊÇLists¶ÔÏó£¬ Ôò¿ÉÒÔͨ¹ýp[1] = '123'ÕâÖֵķ½Ê½¸Ä±äpµÄÄÚÈÝ¡£
ListsÊÇÐòÁÐÈÝÆ÷£¬Ö§³ÖÈκÎÐòÁÐ ......