可以通過管道,可以通過操作系統(tǒng)標準輸入輸出傳遞 等等等等
成都創(chuàng)新互聯(lián)公司長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為全椒企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計,全椒網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
總之操作系統(tǒng)就是php和python之間的bridge
如果說我所看的比笛卡爾更遠一點,那是因為站在巨人肩上的緣故。
python函數(shù)的作用是:
1、函數(shù)其實是把某個功能的代碼封裝到一個代碼塊中,用來為某個重復使用的功能做調(diào)用的一個代碼塊,可以稱為一個函數(shù)的代碼封裝。可以在自定義函數(shù)的小括號中傳入多個參數(shù)。
2、形參:在定義函數(shù)時,小括號中的參數(shù)名稱。實參:在函數(shù)名稱的小括號中,傳入實際的值代替了形參的這個值。函數(shù)可以有返回值(使用return進行返回),也可以沒有返回值。
3、形參可以當做函數(shù)內(nèi)部的一個變量使用,往往只在函數(shù)內(nèi)部進行使用,不影響函數(shù)外部的相同名稱的變量。
4、在函數(shù)內(nèi)部可以返回某個值。直接在函數(shù)內(nèi)部退出來,而不再繼續(xù)執(zhí)行函數(shù)下面的代碼。
更多關(guān)于python函數(shù)的作用,進入:查看更多內(nèi)容
Python是解釋性語言, 底層就是用c實現(xiàn)的, 所以用python調(diào)用C是很容易的, 下面就總結(jié)一下各種調(diào)用的方法, 給出例子, 所有例子都在ubuntu9.10, python2.6下試過
1. Python 調(diào)用 C (base)
想在python中調(diào)用c函數(shù), 如這兒的fact
#include Python.h
int fact(int n)
{
if (n = 1)
return 1;
else
return n * fact(n - 1);
}
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
int n, result;
if (! PyArg_ParseTuple(args, "i:fact", n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
{NULL, NULL}
};
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
把這段代碼存為wrapper.c, 編成so庫,
gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
然后在有此so庫的目錄, 進入python, 可以如下使用
import example
example.fact(4)
2. Python 調(diào)用 C++ (base)
在python中調(diào)用C++類成員函數(shù), 如下調(diào)用TestFact類中的fact函數(shù),
#include Python.h
class TestFact{
public:
TestFact(){};
~TestFact(){};
int fact(int n);
};
int TestFact::fact(int n)
{
if (n = 1)
return 1;
else
return n * (n - 1);
}
int fact(int n)
{
TestFact t;
return t.fact(n);
}
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
int n, result;
if (! PyArg_ParseTuple(args, "i:fact", n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
{NULL, NULL}
};
extern "C" //不加會導致找不到initexample
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
把這段代碼存為wrapper.cpp, 編成so庫,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
然后在有此so庫的目錄, 進入python, 可以如下使用
import example
example.fact(4)
3. Python 調(diào)用 C++ (Boost.Python)
Boost庫是非常強大的庫, 其中的python庫可以用來封裝c++被python調(diào)用, 功能比較強大, 不但可以封裝函數(shù)還能封裝類, 類成員.
首先在ubuntu下安裝boost.python, apt-get install libboost-python-dev
#include boost/python.hpp
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
把代碼存為hello.cpp, 編譯成so庫
g++ hello.cpp -o hello.so -shared -I/usr/include/python2.5 -I/usr/lib/python2.5/config -lboost_python-gcc42-mt-1_34_1
此處python路徑設(shè)為你的python路徑, 并且必須加-lboost_python-gcc42-mt-1_34_1, 這個庫名不一定是這個, 去/user/lib查
然后在有此so庫的目錄, 進入python, 可以如下使用
import hello
hello.greet()
'hello, world'
4. python 調(diào)用 c++ (ctypes)
ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.
ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.
#include Python.h
class TestFact{
public:
TestFact(){};
~TestFact(){};
int fact(int n);
};
int TestFact::fact(int n)
{
if (n = 1)
return 1;
else
return n * (n - 1);
}
extern "C"
int fact(int n)
{
TestFact t;
return t.fact(n);
}
將代碼存為wrapper.cpp不用寫python接口封裝, 直接編譯成so庫,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
進入python, 可以如下使用
import ctypes
pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')
pdll.fact(4)
12
最簡單的就是直接到python官網(wǎng)查看文檔了
python2:
python3:
如果再離線的情況下使用help函數(shù)也可以:
import?re
help(re)
如果解決了您的問題請采納!
如果未解決請繼續(xù)追問
本文名稱:pythoncom函數(shù) Pythoncom
文章來源:http://www.yijiale78.com/article44/dooisee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、手機網(wǎng)站建設(shè)、微信小程序、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)