自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C調(diào)用Python函數(shù)相關(guān)代碼示例剖析

開(kāi)發(fā) 后端
C調(diào)用Python函數(shù)的相關(guān)操作將會(huì)在這篇文章中通過(guò)一段代碼示例來(lái)為大家詳細(xì)介紹。初學(xué)者們可以通過(guò)這里介紹的內(nèi)容充分掌握這一應(yīng)用技巧。

我們?cè)谑褂肅語(yǔ)言的時(shí)候,有時(shí)會(huì)遇到需要調(diào)用Python函數(shù)來(lái)完成一些特定的功能。那么接下來(lái),我們將會(huì)在這里為大家詳細(xì)介紹一下C調(diào)用Python函數(shù)的相關(guān)操作方法,希望可以給大家?guī)?lái)一些幫助。

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對(duì)其  
  9. //進(jìn)行初始化。它會(huì)載入Python的內(nèi)建模塊并添加系統(tǒng)路  
  10. //徑到模塊搜索路徑中。這個(gè)函數(shù)沒(méi)有返回值,檢查系統(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對(duì)象。當(dāng)需要從  
  45. // C++傳遞變量到Python時(shí),就會(huì)使用這個(gè)函數(shù)。此函數(shù)  
  46. // 有點(diǎn)類似C的printf,但格式不同。常用的格式有  
  47. // s 表示字符串,  
  48. // i 表示整型變量,  
  49. // f 表示浮點(diǎn)數(shù),  
  50. // O 表示一個(gè)Python對(duì)象。  
  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;  

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

【編輯推薦】

  1. Python插件PyDev正確配置方法解讀
  2. Python future模塊常見(jiàn)示例相關(guān)解讀
  3. Python調(diào)用zip命令正確操作方法解析
  4. Python元組創(chuàng)建方法及特殊性解析
  5. Python運(yùn)算符基本類型總結(jié)
責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-02-03 10:05:48

C++ enum枚舉

2010-03-19 14:44:30

Python模塊級(jí)函數(shù)

2010-02-05 10:23:09

C++基本函數(shù)

2010-03-04 09:40:52

Python Clas

2010-02-04 16:07:39

C++回調(diào)函數(shù)

2009-08-27 15:53:30

C#中using wo

2010-03-22 15:18:27

2009-09-01 11:04:59

C#調(diào)用擴(kuò)展方法

2010-02-25 13:40:17

WCF禁用安全配置

2009-12-28 08:54:58

ADO錯(cuò)誤

2010-03-05 09:49:34

Python文件操作

2010-03-18 09:33:46

python隨機(jī)數(shù)模塊

2022-07-18 15:32:37

C++虛函數(shù)表

2010-02-05 17:09:19

C++創(chuàng)建Web服務(wù)

2009-09-01 16:49:56

C#文件上傳下載

2010-03-22 17:53:50

Python字符Python字符串

2010-02-01 11:22:09

C++虛函數(shù)

2009-10-15 17:50:48

VB.NET調(diào)用API

2010-01-27 13:38:29

C++ Sum函數(shù)

2010-02-06 16:39:45

C++ assert(
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)