隨筆-208  評(píng)論-469  文章-30  trackbacks-0



          代碼如下,分別演示直接執(zhí)行python語(yǔ)句、無(wú)返回?zé)o參數(shù)函數(shù)調(diào)用、返回單參數(shù)函數(shù)調(diào)用。返回多參數(shù)函數(shù)調(diào)用:

          #include <Python.h>
          #include <iostream>
          using namespace std;

          //執(zhí)行python命令
          void ExecPythonCommand()
          {
           //直接執(zhí)行
           PyRun_SimpleString("from time import time,ctime\n"
            "print 'Today is',ctime(time())\n");
          }

          //調(diào)用無(wú)參數(shù)函數(shù)
          void InvokeNoParm()
          {
           PyObject* pMod = NULL;
           PyObject* pFunc = NULL;
           //導(dǎo)入模塊
           pMod = PyImport_ImportModule("Life");
           if(pMod)
           {
            //獲取函數(shù)地址
            pFunc = PyObject_GetAttrString(pMod, "a");
            if(pFunc)
            {
             //函數(shù)調(diào)用
             PyEval_CallObject(pFunc, NULL);
            }
            else
            {
             cout << "cannot find function a" << endl;
            }
           }
           else
           {
            cout << "cannot find Life.py" << endl;
           }
          }

          //調(diào)用一參數(shù)函數(shù)
          void InvokeWith1Parm()
          {
           PyObject* pMod = NULL;
           PyObject* pFunc = NULL;
           PyObject* pParm = NULL;
           PyObject* pRetVal = NULL;
           int   iRetVal = 0;
           //導(dǎo)入模塊
           pMod = PyImport_ImportModule("FuncDef");
           if(pMod)
           {
            pFunc = PyObject_GetAttrString(pMod, "square");
            if(pFunc)
            {
             //創(chuàng)建參數(shù)
             pParm = Py_BuildValue("(i)", 5);
             //函數(shù)調(diào)用
             pRetVal = PyEval_CallObject(pFunc, pParm);
             //解析返回值
             PyArg_Parse(pRetVal, "i", &iRetVal);
             cout << "square 5 is: " << iRetVal << endl;
            }
            else
            {
             cout << "cannot find function square" << endl;
            }
           }
           else
           {
            cout << "cannot find FuncDef.py" << endl;
           }
          }

          //調(diào)用多參數(shù)函數(shù)
          void InvokeWith2Parm()
          {
           PyObject* pMod = NULL;
           PyObject* pFunc = NULL;
           PyObject* pParm = NULL;
           PyObject* pRetVal = NULL;
           int   iRetVal = 0;
           //導(dǎo)入模塊
           pMod = PyImport_ImportModule("add");
           if(pMod)
           {
            pFunc = PyObject_GetAttrString(pMod, "add");
            if(pFunc)
            {
             //創(chuàng)建兩個(gè)參數(shù)
             pParm = PyTuple_New(2);
             //為參數(shù)賦值
             PyTuple_SetItem(pParm, 0, Py_BuildValue("i",2000));
             PyTuple_SetItem(pParm, 1, Py_BuildValue("i",3000));
             //函數(shù)調(diào)用
             pRetVal = PyEval_CallObject(pFunc, pParm);
             //解析返回值
             PyArg_Parse(pRetVal, "i", &iRetVal);
             cout << "2000 + 3000 = " << iRetVal << endl;
            }
            else
            {
             cout << "cannot find function square" << endl;
            }
           }
           else
           {
            cout << "cannot find add.py" << endl;
           }
          }

          int main(int argc, char* argv[])
          {
           Py_Initialize(); //python 解釋器的初始化
           
           ExecPythonCommand();
           InvokeNoParm();
           InvokeWith1Parm();
           InvokeWith2Parm();

           Py_Finalize();  // 垃圾回收、清除導(dǎo)入庫(kù)
           return 0;
          }


          習(xí)慣C++的內(nèi)存分配釋放,突然間不用釋放,感覺(jué)很蹊蹺,上網(wǎng)查發(fā)現(xiàn)也沒(méi)有釋放函數(shù)。如果真這樣的話,是很可怕的,因?yàn)闊o(wú)法自己管理內(nèi)存,但是我相信編譯器作者的垃圾回收機(jī)制,所以O(shè)K,不管!!

          代碼下載

          posted on 2006-01-17 20:04 EricWong 閱讀(669) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): C&C++

          評(píng)論:
          # re: C++之python函數(shù)調(diào)用 2007-09-04 17:39 | 金慶
          應(yīng)該調(diào)用Py_DECREF()釋放內(nèi)存,
          例如:
          Py_DECREF(pMod);

          不然Python不會(huì)進(jìn)行垃圾回收,因?yàn)樗吹綄?duì)象仍被引用。  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 锡林郭勒盟| 双江| 筠连县| 中卫市| 个旧市| 阿尔山市| 桐庐县| 乐业县| 周至县| 宣汉县| 项城市| 阜南县| 西峡县| 和林格尔县| 德惠市| 牙克石市| 芒康县| 永胜县| 卢氏县| 策勒县| 义马市| 南华县| 黄浦区| 盈江县| 额尔古纳市| 江达县| 辽源市| 临澧县| 历史| 宁武县| 枝江市| 大石桥市| 宁陕县| 庄河市| 长汀县| 临桂县| 济源市| 雷州市| 聊城市| 肇东市| 栾城县|