python訪問函數(shù)代碼 python訪問api

python的調(diào)用函數(shù)怎么用?

注意代碼格式

創(chuàng)新互聯(lián)長期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為福安企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),福安網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

python以縮進(jìn)為標(biāo)準(zhǔn) 而不是像Java 以分號分隔

函數(shù)調(diào)用需要 寫在main函數(shù)內(nèi)

仔細(xì)檢查你的代碼格式和語法

希望可以幫助你? 請采納? 謝謝

python怎么查看函數(shù)代碼

我們經(jīng)常會用到python的內(nèi)置函數(shù),但python庫中的內(nèi)置函數(shù)何其之多,有時候難免會忘了這個函數(shù)的功能。這時候我們可以在

pycharm中把鼠標(biāo)定位到這個函數(shù),然后用快捷鍵Ctrl+B去查看:

更多技術(shù)請關(guān)注Python視頻教程。

python調(diào)用c函數(shù)

Python是解釋性語言, 底層就是用c實(shí)現(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庫的目錄, 進(jìn)入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" //不加會導(dǎo)致找不到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庫的目錄, 進(jìn)入python, 可以如下使用

import example

example.fact(4)

3. Python 調(diào)用 C++ (Boost.Python)

Boost庫是非常強(qiáng)大的庫, 其中的python庫可以用來封裝c++被python調(diào)用, 功能比較強(qiáng)大, 不但可以封裝函數(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庫的目錄, 進(jìn)入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

進(jìn)入python, 可以如下使用

import ctypes

pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')

pdll.fact(4)

12

如何在Python中調(diào)用C++代碼或Java包中的函數(shù)

可以用Python的擴(kuò)展來實(shí)現(xiàn)。可參考Extending Python with C or C++。

Python本來是C實(shí)現(xiàn)的,封裝二進(jìn)制兼容的C++是很容易的。

Java的話得通過JNI來實(shí)現(xiàn),就是說在Python擴(kuò)展里用C調(diào)用Java。

另外,你也可以寫一個TCP服務(wù)來包裝C++/Java的接口,通過網(wǎng)絡(luò)來調(diào)用,這樣更通用。

網(wǎng)站名稱:python訪問函數(shù)代碼 python訪問api
本文來源:http://muchs.cn/article22/dosjgjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、軟件開發(fā)、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司Google、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名