日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C調(diào)用Python函數(shù)相關(guān)代碼示例剖析

我們在使用C語言的時(shí)候,有時(shí)會遇到需要調(diào)用Python函數(shù)來完成一些特定的功能。那么接下來,我們將會在這里為大家詳細(xì)介紹一下C調(diào)用Python函數(shù)的相關(guān)操作方法,希望可以給大家?guī)硪恍椭?/p>

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括連江網(wǎng)站建設(shè)、連江網(wǎng)站制作、連江網(wǎng)頁制作以及連江網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,連江網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到連江省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

Python腳本,存為pytest.py

 
 
 
  1. def add(a,b):  
  2. print "in python function add"  
  3. print "a = " + str(a)  
  4. print "b = " + str(b)  
  5. print "ret = " + str(a+b)  
  6. return a + b 

C調(diào)用Python函數(shù)的代碼示例:

 
 
 
  1. #include < stdio.h> 
  2. #include < stdlib.h> 
  3. #include "C:/Python26/include/python.h"  
  4. #pragma comment(lib, "C:\\Python26\\libs\\python26.lib")  
  5. int main(int argc, char** argv)  
  6. {  
  7. // 初始化Python  
  8. //在使用Python系統(tǒng)前,必須使用Py_Initialize對其  
  9. //進(jìn)行初始化。它會載入Python的內(nèi)建模塊并添加系統(tǒng)路  
  10. //徑到模塊搜索路徑中。這個(gè)函數(shù)沒有返回值,檢查系統(tǒng)  
  11. //是否初始化成功需要使用Py_IsInitialized。  
  12. PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pRetVal;  
  13. Py_Initialize();  
  14. // 檢查初始化是否成功  
  15. if ( !Py_IsInitialized() )   
  16. {  
  17. return -1;  
  18. }  
  19. // 載入名為pytest的腳本(注意:不是pytest.py)  
  20. pName = PyString_FromString("pytest");  
  21. pModule = PyImport_Import(pName);  
  22. if ( !pModule )  
  23. {  
  24. printf("can't find pytest.py");  
  25. getchar();  
  26. return -1;  
  27. }  
  28. pDict = PyModule_GetDict(pModule);  
  29. if ( !pDict )   
  30. {  
  31. return -1;  
  32. }  
  33. // 找出函數(shù)名為add的函數(shù)  
  34. pFunc = PyDict_GetItemString(pDict, "add");  
  35. if ( !pFunc || !PyCallable_Check(pFunc) )  
  36. {  
  37. printf("can't find function [add]");  
  38. getchar();  
  39. return -1;  
  40. }  
  41. // 參數(shù)進(jìn)棧  
  42. pArgs = PyTuple_New(2);  
  43. // PyObject* Py_BuildValue(char *format, ...)  
  44. // 把C++的變量轉(zhuǎn)換成一個(gè)Python對象。當(dāng)需要從  
  45. // C++傳遞變量到Python時(shí),就會使用這個(gè)函數(shù)。此函數(shù)  
  46. // 有點(diǎn)類似C的printf,但格式不同。常用的格式有  
  47. // s 表示字符串,  
  48. // i 表示整型變量,  
  49. // f 表示浮點(diǎn)數(shù),  
  50. // O 表示一個(gè)Python對象。  
  51. PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));   
  52. PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));  
  53. // 調(diào)用Python函數(shù)  
  54. pRetVal = PyObject_CallObject(pFunc, pArgs);  
  55. printf("function return value : %ld\r\n", PyInt_AsLong(pRetVal));  
  56. Py_DECREF(pName);  
  57. Py_DECREF(pArgs);  
  58. Py_DECREF(pModule);  
  59. Py_DECREF(pRetVal);  
  60. // 關(guān)閉Python  
  61. Py_Finalize();  
  62. return 0;  
  63. }  
  64. //一下為個(gè)人實(shí)踐的另一套方法  
  65. #include < Python.h> 
  66. #include < conio.h> 
  67. int main()  
  68. {  
  69. Py_Initialize();  
  70. if (!Py_IsInitialized())  
  71. {  
  72. printf("初始化錯(cuò)誤\n");  
  73. return -1;  
  74. }  
  75. PyObject* pModule = NULL;  
  76. PyObject* pFunc = NULL;  
  77. PyObject* pArg = NULL;  
  78. PyObject* pRetVal = NULL;  
  79. pModule = PyImport_ImportModule("hello");  
  80. pFunc = PyObject_GetAttrString(pModule,"hello");  
  81. pArg = Py_BuildValue("(i,i)",33,44);  
  82. pRetVal = PyObject_CallObject(pFunc,pArg);  
  83. printf("%d\n",PyInt_AsLong(pRetVal));  
  84. Py_Finalize();  
  85. _getch();  
  86. return 0;  

以上就是我們對C調(diào)用Python函數(shù)的相關(guān)操作方法的介紹。


標(biāo)題名稱:C調(diào)用Python函數(shù)相關(guān)代碼示例剖析
分享鏈接:http://www.5511xx.com/article/cdodhsp.html